Mojo struct
StringLiteral
This type represents a string literal.
String literals are all null-terminated for compatibility with C APIs, but this is subject to change. String literals store their length as an integer, and this does not include the null terminator.
Aliasesβ
type = string
:
Fieldsβ
- βvalue (
string
): The underlying storage for the string literal.
Implemented traitsβ
AnyType
,
Boolable
,
CollectionElement
,
CollectionElementNew
,
Comparable
,
Copyable
,
EqualityComparable
,
ExplicitlyCopyable
,
Formattable
,
Hashable
,
IntableRaising
,
KeyElement
,
Movable
,
Representable
,
Sized
,
Stringable
Methodsβ
__init__
β
__init__(inout self: Self, value: string)
Create a string literal from a builtin string type.
Args:
- βvalue (
string
): The string value.
__init__(inout self: Self, *, other: Self)
Copy constructor.
Args:
- βother (
Self
): The string literal to copy.
__bool__
β
__bool__(self: Self) -> Bool
Convert the string to a bool value.
Returns:
True if the string is not empty.
__lt__
β
__lt__(self: Self, rhs: Self) -> Bool
Compare this StringLiteral to the RHS using LT comparison.
Args:
- βrhs (
Self
): The other StringLiteral to compare against.
Returns:
True if this StringLiteral is strictly less than the RHS StringLiteral and False otherwise.
__le__
β
__le__(self: Self, rhs: Self) -> Bool
Compare this StringLiteral to the RHS using LE comparison.
Args:
- βrhs (
Self
): The other StringLiteral to compare against.
Returns:
True if this StringLiteral is less than or equal to the RHS StringLiteral and False otherwise.
__eq__
β
__eq__(self: Self, rhs: Self) -> Bool
Compare two string literals for equality.
Args:
- βrhs (
Self
): The string to compare.
Returns:
True if they are equal.
__ne__
β
__ne__(self: Self, rhs: Self) -> Bool
Compare two string literals for inequality.
Args:
- βrhs (
Self
): The string to compare.
Returns:
True if they are not equal.
__gt__
β
__gt__(self: Self, rhs: Self) -> Bool
Compare this StringLiteral to the RHS using GT comparison.
Args:
- βrhs (
Self
): The other StringLiteral to compare against.
Returns:
True if this StringLiteral is strictly greater than the RHS StringLiteral and False otherwise.
__ge__
β
__ge__(self: Self, rhs: Self) -> Bool
Compare this StringLiteral to the RHS using GE comparison.
Args:
- βrhs (
Self
): The other StringLiteral to compare against.
Returns:
True if this StringLiteral is greater than or equal to the RHS StringLiteral and False otherwise.
__contains__
β
__contains__(self: 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: Self, rhs: Self) -> Self
Concatenate two string literals.
Args:
- βrhs (
Self
): The string to concat.
Returns:
The concatenated string.
__len__
β
__len__(self: Self) -> Int
Get the string length.
Returns:
The length of this StringLiteral.
__int__
β
__int__(self: Self) -> Int
Parses the given string as a base-10 integer and returns that value.
For example, int("19")
returns 19
. If the given string cannot be parsed
as an integer value, an error is raised. For example, int("hi")
raises an
error.
Returns:
An integer value that represents the string, or otherwise raises.
__str__
β
__str__(self: Self) -> String
Convert the string literal to a string.
Returns:
A new string.
__repr__
β
__repr__(self: Self) -> String
Return a representation of the StringLiteral
instance.
You don't need to call this method directly, use repr("...")
instead.
Returns:
A new representation of the string.
__hash__
β
__hash__(self: Self) -> UInt
Hash the underlying buffer using builtin hash.
Returns:
A 64-bit hash value. This value is not suitable for cryptographic uses. Its intended usage is for data structures. See the hash
builtin documentation for more details.
__fspath__
β
__fspath__(self: Self) -> String
Return the file system path representation of the object.
Returns:
The file system path representation as a string.
byte_length
β
byte_length(self: Self) -> Int
Get the string length in bytes.
Notes: This does not include the trailing null terminator in the count.
Returns:
The length of this StringLiteral in bytes.
unsafe_ptr
β
unsafe_ptr(self: Self) -> UnsafePointer[SIMD[uint8, 1], 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1]
Get raw pointer to the underlying data.
Returns:
The raw pointer to the data.
unsafe_cstr_ptr
β
unsafe_cstr_ptr(self: Self) -> UnsafePointer[SIMD[int8, 1], 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1]
Retrieves a C-string-compatible pointer to the underlying memory.
The returned pointer is guaranteed to be NUL terminated, and not null.
Returns:
The pointer to the underlying memory.
as_string_slice
β
as_string_slice(self: Self) -> StringSlice[0, MutableStaticLifetime]
Returns a string slice of this static string literal.
Returns:
A string slice pointing to this static string literal.
as_bytes_slice
β
as_bytes_slice(self: Self) -> Span[0, SIMD[uint8, 1], MutableStaticLifetime]
Returns a contiguous slice of the bytes owned by this string.
Returns:
A contiguous slice pointing to the bytes owned by this string.
format_to
β
format_to(self: Self, inout writer: Formatter)
Formats this string literal to the provided formatter.
Args:
- βwriter (
Formatter
): The formatter to write to.
find
β
find(self: Self, substr: Self, start: Int = 0) -> Int
Finds the offset of the first occurrence of substr
starting at start
. If not found, returns -1.
Args:
- βsubstr (
Self
): The substring to find. - βstart (
Int
): The offset from which to find.
Returns:
The offset of substr
relative to the beginning of the string.
rfind
β
rfind(self: Self, substr: Self, start: Int = 0) -> Int
Finds the offset of the last occurrence of substr
starting at start
. If not found, returns -1.
Args:
- βsubstr (
Self
): The substring to find. - βstart (
Int
): The offset from which to find.
Returns:
The offset of substr
relative to the beginning of the string.
replace
β
replace(self: Self, old: Self, new: Self) -> Self
Return a copy of the string with all occurrences of substring old
if replaced by new
. This operation only works in the param domain.
Args:
- βold (
Self
): The substring to replace. - βnew (
Self
): The substring to replace with.
Returns:
The string where all occurrences of old
are replaced with new
.
join
β
join[T: StringableCollectionElement](self: Self, elems: List[T, hint_trivial_type]) -> String
Joins string elements using the current string as a delimiter.
Parameters:
- βT (
StringableCollectionElement
): The types of the elements.
Args:
- βelems (
List[T, hint_trivial_type]
): The input values.
Returns:
The joined string.
lower
β
lower(self: Self) -> String
Returns a copy of the string literal with all cased characters converted to lowercase.
Returns:
A new string where cased letters have been converted to lowercase.
upper
β
upper(self: Self) -> String
Returns a copy of the string literal with all cased characters converted to uppercase.
Returns:
A new string where cased letters have been converted to uppercase.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!
π What went wrong?