String¶
A dynamically-sized UTF-8 byte string type.
Module: Std
Summary¶
struct String {
data: *char8;
length: uint;
}
String::New()
String::From(str: *const char8, length: uint)
String::From(slice: char8[])
func Clone(self) -> String
func operator +(self, other: String) -> String
func operator +(self, slice: char8[]) -> String
func ToString(self) -> String
func Data(self) -> *char8
func Length(self) -> uint
func IsEmpty(self) -> bool8
func ToUpper(self) -> String
func ToLower(self) -> String
func Capitalize(self) -> String
func TitleCase(self) -> String
func Trim(self) -> String
func StartsWith(self, prefix: String) -> bool
func EndsWith(self, suffix: String) -> bool
func Split(self, delimiter: char8) -> StringArray
func Repeat(self, count: uint) -> String
func ToUpperInPlace(self)
func ToLowerInPlace(self)
func CapitalizeInPlace(self)
func TrimInPlace(self)
func TitleCaseInPlace(self)
Description¶
String represents a sequence of bytes stored in dynamically allocated memory.
Strings are immutable by convention, although some methods provide in-place modification of the underlying data.
String implements the Display interface.
Structures¶
String¶
| Field | Description |
|---|---|
data |
Pointer to the character buffer. |
length |
Number of bytes in the string. |
StringArray¶
| Field | Description |
|---|---|
data |
Pointer to the array of strings. |
length |
Number of strings in the array. |
Construction¶
New¶
Creates an empty string.
Example¶
From¶
From Pointer¶
Creates a string by copying raw character data.
Example¶
From Slice¶
Creates a string by copying a character slice.
Example¶
Clone¶
Returns a copy of the string.
Example¶
Operators¶
Concatenation¶
String + String¶
Returns a new string containing both strings concatenated.
Example¶
String + char8[ ]¶
Returns a new string containing the string followed by the character slice.
Example¶
Accessors¶
Data¶
Returns a pointer to the underlying character buffer.
Length¶
Returns the number of bytes stored in the string.
IsEmpty¶
Returns true if the string length is zero.
Example¶
Case Conversion¶
ToUpper¶
Returns a new uppercase string.
Example¶
ToLower¶
Returns a new lowercase string.
Capitalize¶
Returns a new string with the first character uppercase and the remaining characters lowercase.
Example¶
TitleCase¶
Returns a new string with the first letter of each word capitalized.
Example¶
Whitespace¶
Trim¶
Returns a copy of the string with leading and trailing whitespace removed.
Whitespace characters recognized:
- Space (
' ') - Tab (
'\t') - Newline (
'\n') - Carriage return (
'\r')
Example¶
Search¶
StartsWith¶
Returns true if the string begins with the specified prefix.
Example¶
EndsWith¶
Returns true if the string ends with the specified suffix.
Example¶
Splitting¶
Split¶
Splits the string into multiple substrings separated by the specified delimiter.
Example¶
Result¶
Notes¶
- The returned strings reference the original string's data.
- No character data is copied.
- The original string must remain valid while the returned
StringArrayis in use.
Repetition¶
Repeat¶
Returns a new string containing the original string repeated count times.
Internally, Repeat uses StringBuilder to efficiently construct the resulting string, reducing unnecessary memory allocations and copies.
Example¶
Result:
In-Place Operations¶
These functions modify the existing string buffer directly.
ToUpperInPlace¶
Converts all characters to uppercase.
ToLowerInPlace¶
Converts all characters to lowercase.
CapitalizeInPlace¶
Capitalizes the first character and lowercases the remaining characters.
TrimInPlace¶
Removes leading and trailing whitespace from the existing buffer.
TitleCaseInPlace¶
Capitalizes the first letter of each word directly in the existing buffer.
ToString¶
Returns the string itself.
This method exists to satisfy the Display interface.
Display Implementation¶
String implements the Display interface.
Calling ToString on a String returns the string itself.