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?