Mojo struct
String
struct String
Represents a mutable string.
Aliases
ASCII_LOWERCASE = String("abcdefghijklmnopqrstuvwxyz")
:ASCII_UPPERCASE = String("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
:ASCII_LETTERS = String("abcdefghijklmnopqrstuvwxyz").__add__(String("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
:DIGITS = String("0123456789")
:HEX_DIGITS = String("0123456789").__add__(String("abcdef")).__add__(String("ABCDEF"))
:OCT_DIGITS = String("01234567")
:PUNCTUATION = String("!\22#$%&'()*+,-./:;<=>?@[\\]^_
{|}~")`:PRINTABLE = String("0123456789").__add__(String("abcdefghijklmnopqrstuvwxyz").__add__(String("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))).__add__(String("!\22#$%&'()*+,-./:;<=>?@[\\]^_
{|}~")).add(" \09\0A\0D\0B\0C")`:
Implemented traits
AnyType
,
AsBytes
,
Boolable
,
BytesCollectionElement
,
CollectionElement
,
CollectionElementNew
,
Comparable
,
Copyable
,
EqualityComparable
,
ExplicitlyCopyable
,
FloatableRaising
,
Hashable
,
IntableRaising
,
KeyElement
,
Movable
,
Representable
,
Sized
,
Stringable
,
UnknownDestructibility
,
Writable
,
Writer
,
_HashableWithHasher
Methods
__init__
__init__(out self, owned impl: List[SIMD[uint8, 1], hint_trivial_type])
Construct a string from a buffer of bytes without copying the allocated data.
The buffer must be terminated with a null byte:
var buf = List[UInt8]()
buf.append(ord('H'))
buf.append(ord('i'))
buf.append(0)
var hi = String(buf)
var buf = List[UInt8]()
buf.append(ord('H'))
buf.append(ord('i'))
buf.append(0)
var hi = String(buf)
Args:
- impl (
List[SIMD[uint8, 1], hint_trivial_type]
): The buffer.
__init__(out self, impl: List[SIMD[uint8, 1], True])
Construct a string from a buffer of bytes, copying the allocated data. Use the transfer operator ^ to avoid the copy.
The buffer must be terminated with a null byte:
var buf = List[UInt8]()
buf.append(ord('H'))
buf.append(ord('i'))
buf.append(0)
var hi = String(buf)
var buf = List[UInt8]()
buf.append(ord('H'))
buf.append(ord('i'))
buf.append(0)
var hi = String(buf)
Args:
- impl (
List[SIMD[uint8, 1], True]
): The buffer.
__init__(out self)
Construct an uninitialized string.
__init__(out self, *, capacity: Int)
Construct an uninitialized string with the given capacity.
Args:
- capacity (
Int
): The capacity of the string.
__init__(out self, *, other: Self)
Explicitly copy the provided value.
Args:
- other (
Self
): The value to copy.
__init__(out self, str: StringRef)
Construct a string from a StringRef object.
Args:
- str (
StringRef
): The StringRef from which to construct this string object.
__init__(out self, str_slice: StringSlice[origin])
Construct a string from a string slice.
This will allocate a new string that copies the string contents from
the provided string slice str_slice
.
Args:
- str_slice (
StringSlice[origin]
): The string slice from which to construct this string.
__init__(out self, literal: StringLiteral)
Constructs a String value given a constant string.
Args:
- literal (
StringLiteral
): The input constant string.
__init__(out self, *, ptr: UnsafePointer[SIMD[uint8, 1]], length: Int)
Creates a string from the buffer. Note that the string now owns the buffer.
The buffer must be terminated with a null byte.
Args:
- ptr (
UnsafePointer[SIMD[uint8, 1]]
): The pointer to the buffer. - length (
Int
): The length of the buffer, including the null terminator.
__bool__
__bool__(self) -> Bool
Checks if the string is not empty.
Returns:
True if the string length is greater than zero, and False otherwise.
__getitem__
__getitem__[IndexerType: Indexer](self, idx: IndexerType) -> Self
Gets the character at the specified position.
Parameters:
- IndexerType (
Indexer
): The inferred type of an indexer argument.
Args:
- idx (
IndexerType
): The index value.
Returns:
A new string containing the character at the specified position.
__getitem__(self, span: Slice) -> Self
Gets the sequence of characters at the specified positions.
Args:
- span (
Slice
): A slice that specifies positions of the new substring.
Returns:
A new string containing the string at the specified positions.
__lt__
__lt__(self, rhs: Self) -> Bool
Compare this String to the RHS using LT comparison.
Args:
- rhs (
Self
): The other String to compare against.
Returns:
True if this String is strictly less than the RHS String and False otherwise.
__le__
__le__(self, rhs: Self) -> Bool
Compare this String to the RHS using LE comparison.
Args:
- rhs (
Self
): The other String to compare against.
Returns:
True iff this String is less than or equal to the RHS String.
__eq__
__eq__(self, other: Self) -> Bool
Compares two Strings if they have the same values.
Args:
- other (
Self
): The rhs of the operation.
Returns:
True if the Strings are equal and False otherwise.
__ne__
__ne__(self, other: Self) -> Bool
Compares two Strings if they do not have the same values.
Args:
- other (
Self
): The rhs of the operation.
Returns:
True if the Strings are not equal and False otherwise.
__gt__
__gt__(self, rhs: Self) -> Bool
Compare this String to the RHS using GT comparison.
Args:
- rhs (
Self
): The other String to compare against.
Returns:
True iff this String is strictly greater than the RHS String.
__ge__
__ge__(self, rhs: Self) -> Bool
Compare this String to the RHS using GE comparison.
Args:
- rhs (
Self
): The other String to compare against.
Returns:
True iff this String is greater than or equal to the RHS String.
__contains__
__contains__(self, substr: Self) -> Bool
Returns True if the substring is contained within the current string.
Args:
- substr (
Self
): The substring to check.
Returns:
True if the string contains the substring.
__add__
__add__(self, other: Self) -> Self
Creates a string by appending another string at the end.
Args:
- other (
Self
): The string to append.
Returns:
The new constructed string.
__add__(self, other: StringLiteral) -> Self
Creates a string by appending a string literal at the end.
Args:
- other (
StringLiteral
): The string literal to append.
Returns:
The new constructed string.
__add__(self, other: StringSlice[origin]) -> Self
Creates a string by appending a string slice at the end.
Args:
- other (
StringSlice[origin]
): The string slice to append.
Returns:
The new constructed string.
__mul__
__mul__(self, n: Int) -> Self
Concatenates the string n
times.
Args:
- n (
Int
): The number of times to concatenate the string.
Returns:
The string concatenated n
times.
__radd__
__radd__(self, other: Self) -> Self
Creates a string by prepending another string to the start.
Args:
- other (
Self
): The string to prepend.
Returns:
The new constructed string.
__radd__(self, other: StringLiteral) -> Self
Creates a string by prepending another string literal to the start.
Args:
- other (
StringLiteral
): The string to prepend.
Returns:
The new constructed string.
__radd__(self, other: StringSlice[origin]) -> Self
Creates a string by prepending another string slice to the start.
Args:
- other (
StringSlice[origin]
): The string to prepend.
Returns:
The new constructed string.
__iadd__
__iadd__(mut self, other: Self)
Appends another string to this string.
Args:
- other (
Self
): The string to append.
__iadd__(mut self, other: StringLiteral)
Appends another string literal to this string.
Args:
- other (
StringLiteral
): The string to append.
__iadd__(mut self, other: StringSlice[origin])
Appends another string slice to this string.
Args:
- other (
StringSlice[origin]
): The string to append.
write_bytes
write_bytes(mut self, bytes: Span[SIMD[uint8, 1], origin])
Write a byte span to this String.
Args:
- bytes (
Span[SIMD[uint8, 1], origin]
): The byte span to write to this String. Must NOT be null terminated.
write
write[*Ts: Writable](mut self, *args: *Ts)
Write a sequence of Writable arguments to the provided Writer.
Parameters:
- *Ts (
Writable
): Types of the provided argument sequence.
Args:
- *args (
*Ts
): Sequence of arguments to write to this Writer.
static write[*Ts: Writable](*args: *Ts, *, sep: StringSlice[StaticConstantOrigin] = StringSlice(""), end: StringSlice[StaticConstantOrigin] = StringSlice("")) -> Self
Construct a string by concatenating a sequence of Writable arguments.
Examples:
Construct a String from several Writable
arguments:
var string = String.write(1, ", ", 2.0, ", ", "three")
print(string) # "1, 2.0, three"
var string = String.write(1, ", ", 2.0, ", ", "three")
print(string) # "1, 2.0, three"
.
Parameters:
- *Ts (
Writable
): The types of the arguments to format. Each type must be satisfyWritable
.
Args:
- *args (
*Ts
): A sequence of Writable arguments. - sep (
StringSlice[StaticConstantOrigin]
): The separator used between elements. - end (
StringSlice[StaticConstantOrigin]
): The String to write after printing the elements.
Returns:
A string formed by formatting the argument sequence.
static write[*Ts: Writable](args: VariadicPack[origin, Writable, Ts], sep: StringSlice[StaticConstantOrigin] = StringSlice(""), end: StringSlice[StaticConstantOrigin] = StringSlice("")) -> Self
Construct a string by passing a variadic pack.
Examples:
fn variadic_pack_to_string[
*Ts: Writable,
](*args: *Ts) -> String:
return String.write(args)
string = variadic_pack_to_string(1, ", ", 2.0, ", ", "three")
fn variadic_pack_to_string[
*Ts: Writable,
](*args: *Ts) -> String:
return String.write(args)
string = variadic_pack_to_string(1, ", ", 2.0, ", ", "three")
.
Parameters:
- *Ts (
Writable
): The types of the arguments to format. Each type must be satisfyWritable
.
Args:
- args (
VariadicPack[origin, Writable, Ts]
): A VariadicPack of Writable arguments.