String
Module
Implements basic object methods for working with strings.
String
Represent a mutable string.
Aliases:
buffer_type = DynamicVector[SIMD[si8, 1]]
Fields:
buffer
The underlying storage for the string.
Functions:
__init__
__init__(self: Self&)
Construct an empty string.
__init__(self: Self&, str: StringRef)
Construct a string from a StringRef object.
Args:
- str (
StringRef
): The StringRef from which to construct this string object.
__init__(self: Self&, str: StringLiteral)
Construct a String value given a constant string.
Args:
- str (
StringLiteral
): The input constant string.
__init__(self: Self&, val: Bool)
Construct a string representing an bool value.
Args:
- val (
Bool
): The boolean value.
__init__(self: Self&, num: Int)
Construct a string representing an integer value.
Args:
- num (
Int
): The integer value.
__init__(self: Self&, num: FloatLiteral)
Construct a string representing a float value.
Args:
- num (
FloatLiteral
): The float value.
__init__[simd_width: Int, type: DType](self: Self&, simd_vec: SIMD[type, simd_width])
Construct a string for a given SIMD value.
Parameters:
- simd_width (
Int
): The width of the SIMD value. - type (
DType
): The dtype of the SIMD value.
Args:
- simd_vec (
SIMD[type, simd_width]
): The SIMD value.
__init__[size: Int](self: Self&, tuple: StaticIntTuple[size])
Construct a string from a given StaticIntTuple.
Parameters:
- size (
Int
): The size of the tuple.
Args:
- tuple (
StaticIntTuple[size]
): The input tuple.
__init__(self: Self&, ptr: Pointer[SIMD[si8, 1]], len: Int)
Create a string from the buffer. Note that the string now owns the buffer.
Args:
- ptr (
Pointer[SIMD[si8, 1]]
): The pointer to the buffer. - len (
Int
): The length of the buffer.
__copyinit__
__copyinit__(self: Self&, existing: Self)
Create a deep copy of an existing string.
Args:
- existing (
Self
): The string to copy.
__del__
__del__(self: Self)
Deallocate the string.
__bool__
__bool__(self: Self) -> Bool
Check if the string is empty.
Returns:
True if the string is empty and False otherwise.
__getitem__
__getitem__(self: Self, idx: Int) -> Self
Gets the character at the specified position.
Args:
- idx (
Int
): The index value.
Returns:
A new string containing the character at the specified position.
__getitem__(self: 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.
__eq__
__eq__(self: Self, other: Self) -> Bool
Compare 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: Self, other: Self) -> Bool
Compare 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.
__add__
__add__(self: Self, other: Self) -> Self
Create a string by appending another string at the end.
Args:
- other (
Self
): The string to append.
Returns:
The new constructed string.
__iadd__
__iadd__(self: Self&, other: Self)
Append another string to this string.
Args:
- other (
Self
): The string to append.
__len__
__len__(self: Self) -> Int
Return the string length.
Returns:
The string length.
join
join[rank: Int](self: Self, elems: StaticIntTuple[rank]) -> Self
Join the elements from the tuple using the current string as a delimeter.
Parameters:
- rank (
Int
): The size of the tuple.
Args:
- elems (
StaticIntTuple[rank]
): The input tuple.
Returns:
The joined string.
join(self: Self, *lst: Int) -> Self
Join integer elements using the current string as a delimeter.
Args:
- lst (
*Int
): The input values.
Returns:
The joined string.
join(self: Self, *strs: Self) -> Self
Join string elements using the current string as a delimeter.
Args:
- strs (
*Self
): The input values.
Returns:
The joined string.
atol
atol(str: String) -> Int
Parses the given string as a base-10 integer and returns that value.
For example, atol("19")
returns 19
. If the given string cannot be parsed as an integer value, an error is raised. For example, atol("hi")
raises an error.
Args:
- str (
String
): A string to be parsed as a base-10 integer.
Returns:
An integer value that represents the string, or otherwise raises.
chr
chr(c: Int) -> String
Return a string based on the given Unicode code point.
Return the string representing a character whose code point (which must be a positive integer between 0 and 255) is the integer i
. For example, chr(97)
returns the string "a"
. This is the inverse of the ord()
function.
Args:
- c (
Int
): An integer between 0 and 255 that represents a code point.
Returns:
A string containing a single character based on the given code point.
isdigit
isdigit(c: SIMD[si8, 1]) -> Bool
Determine whether the given character is a digit [0-9].
Args:
- c (
SIMD[si8, 1]
): The character to check.
Returns:
True if the character is a digit.
ord
ord(s: String) -> Int
Return an integer that represents the given one-character string.
Given a string representing one ASCII character, return an integer representing the code point of that character. For example, ord("a")
returns the integer 97
. This is the inverse of the chr()
function.
Args:
- s (
String
): The input string, which must contain only a single character.
Returns:
An integer representing the code point of the given character.