Skip to content

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

struct String {
    data: *char8;
    length: uint;
}
Field Description
data Pointer to the character buffer.
length Number of bytes in the string.

StringArray

struct StringArray {
    data: *String;
    length: uint;
}
Field Description
data Pointer to the array of strings.
length Number of strings in the array.

Construction

New

String::New() -> String

Creates an empty string.

Example

let str = String::New();

From

From Pointer

String::From(str: *const char8, length: uint) -> String

Creates a string by copying raw character data.

Example

let str = String::From(ptr, length);

From Slice

String::From(slice: char8[]) -> String

Creates a string by copying a character slice.

Example

let str = String::From("Hello");

Clone

Clone() -> String

Returns a copy of the string.

Example

let copy = original.Clone();

Operators

Concatenation

String + String

stringA + stringB

Returns a new string containing both strings concatenated.

Example

let result = hello + world;

String + char8[ ]

string + slice

Returns a new string containing the string followed by the character slice.

Example

let result = str + " World";

Accessors

Data

Data() -> *char8

Returns a pointer to the underlying character buffer.


Length

Length() -> uint

Returns the number of bytes stored in the string.


IsEmpty

IsEmpty() -> bool8

Returns true if the string length is zero.

Example

if str.IsEmpty() {
    Print("empty");
}

Case Conversion

ToUpper

ToUpper() -> String

Returns a new uppercase string.

Example

let upper = str.ToUpper();

ToLower

ToLower() -> String

Returns a new lowercase string.


Capitalize

Capitalize() -> String

Returns a new string with the first character uppercase and the remaining characters lowercase.

Example

let result = str.Capitalize();

TitleCase

TitleCase() -> String

Returns a new string with the first letter of each word capitalized.

Example

let result = str.TitleCase();

Whitespace

Trim

Trim() -> String

Returns a copy of the string with leading and trailing whitespace removed.

Whitespace characters recognized:

  • Space (' ')
  • Tab ('\t')
  • Newline ('\n')
  • Carriage return ('\r')

Example

let result = str.Trim();

StartsWith

StartsWith(prefix: String) -> bool

Returns true if the string begins with the specified prefix.

Example

if str.StartsWith(String::From("Hello")) {

}

EndsWith

EndsWith(suffix: String) -> bool

Returns true if the string ends with the specified suffix.

Example

if str.EndsWith(String::From(".txt")) {
}

Splitting

Split

Split(delimiter: char8) -> StringArray

Splits the string into multiple substrings separated by the specified delimiter.

Example

let tokens = str.Split(',');

Result

"a,b,c"


["a", "b", "c"]

Notes

  • The returned strings reference the original string's data.
  • No character data is copied.
  • The original string must remain valid while the returned StringArray is in use.

Repetition

Repeat

Repeat(count: uint) -> String

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

let result = String::From("Hi").Repeat(3);

Result:

HiHiHi

In-Place Operations

These functions modify the existing string buffer directly.

ToUpperInPlace

ToUpperInPlace()

Converts all characters to uppercase.


ToLowerInPlace

ToLowerInPlace()

Converts all characters to lowercase.


CapitalizeInPlace

CapitalizeInPlace()

Capitalizes the first character and lowercases the remaining characters.


TrimInPlace

TrimInPlace()

Removes leading and trailing whitespace from the existing buffer.


TitleCaseInPlace

TitleCaseInPlace()

Capitalizes the first letter of each word directly in the existing buffer.

ToString

ToString() -> String

Returns the string itself.

This method exists to satisfy the Display interface.


Display Implementation

String implements the Display interface.

extend String : Display {
    func ToString(self) -> String {
        return *self;
    }
}

Calling ToString on a String returns the string itself.

Example

let str = String::From("Hello");

let text = str.ToString();

See Also