Skip to content

2.9. String Functions

Use the reference tables to find string properties, methods, helpers, and operators, then follow their links to detailed explanations and examples. For storage, conversions, and lifetime rules, see Character and String Types.

2.9.1 Properties and Methods Listing

In the Types column, * means all string types: str, rostr, strslice and embstr.

Properties are read-only. A result of — means that the method modifies the string and returns no value. Lengths, positions, and counts are in bytes, except for the explicitly Unicode-oriented members .wclen and .wchar[...].

Property or method Types Result Purpose
.Add(value) str, embstr — Append text or one char byte
.AddFmt(fmt, args) str, embstr — Append formatted values
.Append(value) str, embstr — Alias for .Add(value)
.AppendChar(ch) str, embstr — Append one char byte
.capacity str int Allocated capacity for text bytes
.Clear() str, embstr — Remove all contents; retain storage
.Clear(free_storage) str — Optionally release the string's storage
.Clone() str str Create an independent copy immediately
.Compact() str — Reduce capacity to the current length
.Contains(needle) * bool Test whether text contains a value
.Delete(index, count = 1) str, embstr — Remove bytes
.EndsWith(suffix) * bool Test the end of the text
.IndexOf(needle, start = 0) * int First matching byte offset, or -1
.Insert(index, value) str, embstr — Insert text or a byte at a byte position
.LastIndexOf(needle) * int Last matching byte offset, or -1
.length * int Current byte length, excluding the terminator
.Lower() * str Copy with ASCII letters in lowercase
.LPad(length, fill) * str Copy with padding on the left
.LTrim()
.LTrim(set)
* str Copy without matching bytes at the left end
.maxlength embstr int Maximum number of text bytes that fit
.pchar * ^char Borrow a pointer to the text bytes
.Pop() str char Remove and return the last byte
.Pop(count) str str Remove and return bytes from the end
.PopFirst() str char Remove and return the first byte
.PopFirst(count) str str Remove and return bytes from the beginning
.Prepend(value) str, embstr — Insert text or a byte at the beginning
.refcount str int Number of references sharing the string handler
.Reserve(capacity) str — Ensure room for at least this many text bytes
.RPad(length, fill) * str Copy with padding on the right
.RTrim()
.RTrim(set)
* str Copy without matching bytes at the right end
.Set(value) str, embstr — Replace the contents
.SetCapacity(capacity) str — Set capacity, shortening the text if necessary
.SetLength(length, fill) str — Resize, filling new bytes with a char
.StartsWith(prefix) * bool Test the beginning of the text
.storage_size embstr int Buffer size including space for the terminator
.ToFloat()
.ToFloat(defvalue)
* float Parse a number; raise on invalid input or return a fallback
.ToInt()
.ToInt(defvalue)
* int Parse an integer; raise on invalid input or return a fallback
.ToWchars() * [*]wchar Decode the whole UTF-8 string into Unicode characters
.Trim()
.Trim(set)
* str Copy without matching bytes at either end
.Truncate(length) str — Shorten without growing
.Upper() * str Copy with ASCII letters in uppercase
.wchar[index] * wchar Read one Unicode character
.wchar[start:end] * [*]wchar Decode a range of Unicode characters
.wclen * int Count Unicode characters in UTF-8 text

Methods returning str require dynamic-string support even when called on an embedded string or borrowed view. Numeric conversion, search methods, embedded-string mutations, and Unicode decoding remain available with dynamic strings disabled. Using str always requires dynamic-string support.

For equivalents of substring(), substr(), slice(), Left(), Right(), or Mid() from other languages, see Byte Indexing and Slicing. Use text[start:end] to extract a substring, or text[start:start + count] when you have a starting position and a byte count. Positions are zero-based byte offsets, and end is excluded. For Unicode character positions, see Unicode Indexing and Slicing.

2.9.2 Standalone String Helpers Listing

These functions are available through the implicit sys runtime interface; ordinary modules need no use declaration. Text parameters accept the string types through their normal conversions. Functions returning str require dynamic-string support.

Function Result Purpose
Format(fmt : strslice, args : varargs) str Create formatted text
PcharLen(pc : ^char)
PcharLen(pc : ^char, maxlen : uint)
int Count bytes in a raw C string, optionally up to a limit
StrFromUtf16(chars : []char16) str Convert a UTF-16 array to UTF-8
StrFromUtf16Fallback(chars : []char16, fallback_wchar : wchar) str Convert UTF-16, replacing malformed sequences
StrFromUtf16Ptr(chars : ^char16, count : int) str Convert an explicit number of UTF-16 code units
StrFromUtf16PtrFallback(chars : ^char16, count : int, fallback_wchar : wchar) str Convert a UTF-16 pointer range with replacement
StrFromWchars(chars : []wchar) str Encode Unicode characters as UTF-8
StrJoin(astr : []str, ajoiner : strslice) str Join strings with a separator between elements
StrLower(text : strslice) str Copy with ASCII letters in lowercase
StrReplace(value : strslice, old_text : strslice, new_text : strslice) str Replace all non-overlapping occurrences of literal text
StrSlice(data : ^char, length : int) strslice Borrow a pointer and an explicit byte count
StrSliceCmp(a : strslice, b : strslice) int Compare two byte ranges
StrSplit(astr : strslice, asplitter : strslice) [*]str Split text at each occurrence of a separator
StrToFloat(text : strslice)
StrToFloat(text : strslice, defvalue : float)
float Parse a number; raise on invalid input or return a fallback
StrToInt(text : strslice)
StrToInt(text : strslice, defvalue : int)
int Parse an integer; raise on invalid input or return a fallback
StrUpper(text : strslice) str Copy with ASCII letters in uppercase

2.9.3 Text Operators

+ concatenates compatible text operands into a new str. += appends to a dynamic string. Both require dynamic-string support.

var greeting : str = "Hello, " + "world"
greeting += "!"           // "Hello, world!"

Using + repeatedly to assemble text creates temporary strings and can cause repeated heap allocations and copies. Prefer .Add() for plain text and .AddFmt() for formatted values, with .Reserve() when the expected size can be estimated.

2.9.4 String Comparison

Text values support ==, <>, <, <=, >, and >= across different string types, including embedded strings with or without an explicit capacity. Strings are compared byte by byte, using unsigned byte values. The first difference determines the order; when one string is a prefix of the other, the shorter string comes first.

var name : str = "alpha"
var limit : rostr = "beta"
var before : bool = name < limit       // true
var prefix : bool = "app" < "apple"    // true
var inside : bool = "a" <= name < "z" // true
var equal : bool = name == "alpha"     // true

Comparison is case-sensitive. It does not validate UTF-8, normalize Unicode, or apply language-specific sorting rules. Known string lengths include internal zero bytes; raw C strings end at their first zero.

2.9.4.1 StrSliceCmp

StrSliceCmp(a : strslice, b : strslice) -> int

StrSliceCmp(a, b) provides the same bytewise comparison as a function. It returns a negative value if a comes before b, zero if equal, and a positive value if a comes after b. It compares the entire known byte range without requiring a terminator.

var order : int = StrSliceCmp("app", "apple") // negative
var same : int = StrSliceCmp("abc", "abc")   // 0
var a : ^char = "alpha"
var b : ^char = "beta"
var before : bool = StrSliceCmp(a, b) < 0     // true: compares text

A raw ^char operand compares text when paired with a string value or, for ordering, a text literal. Two raw pointer variables compare their addresses; use StrSliceCmp to compare their text contents explicitly.

2.9.5 Standalone String Helpers

2.9.5.1 Format

Format(fmt : strslice, args : varargs) -> str

Format(fmt, args) creates a new string using the same placeholders as .AddFmt(). Its strslice format parameter accepts substring views directly, including text without a terminating zero. See Text Formatting for the full syntax.

var label : str = Format("value={:04X}", [255]) // "value=00FF"

2.9.5.2 StrToInt and StrToFloat

StrToInt(text : strslice) -> int
StrToInt(text : strslice, defvalue : int) -> int
StrToFloat(text : strslice) -> float
StrToFloat(text : strslice, defvalue : float) -> float

StrToInt parses decimal digits with an optional leading + or - and rejects values outside the range of int. StrToFloat accepts an optional leading sign, a dot or comma decimal separator, and an optional e/E exponent. An exponent may have a leading sign and must contain digits. The entire input must match; surrounding whitespace and internal zero bytes are rejected. Use .Trim() first when surrounding whitespace should be ignored.

Without defvalue, invalid or empty input raises EStrError. Supplying defvalue returns that fallback on conversion errors. When exceptions are disabled, the no-fallback form reports a runtime error and returns zero if the error handler returns; the fallback form still returns defvalue.

var count : int = StrToInt("+42")              // 42
var fallback : int = StrToInt("invalid", -1)   // -1
var value : float = StrToFloat("1.25e2")       // 125
var comma : float = StrToFloat("1,5")          // 1.5
var invalid : float = StrToFloat("1e+", -1)    // -1: missing exponent digits

2.9.5.3 StrUpper and StrLower

StrUpper(text : strslice) -> str
StrLower(text : strslice) -> str

These functions copy the text, converting only ASCII letters. They leave non-ASCII bytes unchanged and do not modify the source. The methods .Upper() and .Lower() provide the same conversions.

var text : rostr = "Hello, é!"
var upper : str = StrUpper(text) // "HELLO, é!"
var lower : str = StrLower(text) // "hello, é!"

2.9.5.4 StrSplit and StrJoin

StrSplit(astr : strslice, asplitter : strslice) -> [*]str
StrJoin(astr : []str, ajoiner : strslice) -> str

StrSplit matches the whole separator from left to right, without overlapping matches, and returns owned strings. It preserves empty fields, including at the beginning and end. Empty input produces one empty field. An empty separator raises EStrError; with exceptions disabled it reports a runtime error and returns an empty array if the error handler returns.

StrJoin inserts the joiner between array elements, including empty elements. An empty array produces an empty string; an empty joiner concatenates the elements directly. Both functions require dynamic-string support and handle explicit byte ranges, including internal zero bytes.

var parts : [*]str = StrSplit("one::two::::", "::") // ["one", "two", "", ""]
var joined : str = StrJoin(parts, "/")             // "one/two//"

2.9.5.5 StrReplace

StrReplace(value : strslice, old_text : strslice, new_text : strslice) -> str

Returns a string with every non-overlapping occurrence of old_text replaced by new_text, scanning the original input from left to right. Matching is literal and case-sensitive; replacement text is not searched again. An empty new_text removes matches. The input is unchanged.

An empty old_text raises EStrError, consistent with StrSplit's empty separator handling. With exceptions disabled it reports a runtime error and returns an empty string if the error handler returns. The function requires dynamic-string support and accepts explicit byte ranges, including internal zero bytes.

var replaced : str = StrReplace("one::two::", "::", "/") // "one/two/"
var removed : str = StrReplace("a--b--", "--", "")       // "ab"

2.9.5.6 PcharLen

PcharLen(pc : ^char) -> int
PcharLen(pc : ^char, maxlen : uint) -> int

PcharLen(pc) counts bytes up to the first zero byte, excluding that byte. The bounded overload reads at most maxlen bytes and returns maxlen if no zero byte occurs in that range. Both overloads return zero for nil, and a zero limit reads no bytes. The byte count must fit in int.

2.9.5.7 StrSlice

StrSlice(data : ^char, length : int) -> strslice

StrSlice(data, length) borrows exactly length bytes without scanning for a terminator or copying the text. The memory must remain alive and stable. A negative length raises a runtime error; a nil pointer is valid only with a zero length.

var data : [3]char = ['A', char(0), 'B']
var view : strslice = StrSlice(%data[0], data.length)
var bytes : int = view.length // 3: the internal zero does not end the view

2.9.5.8 StrFromWchars

StrFromWchars(chars : []wchar) -> str

StrFromWchars(chars) encodes Unicode characters as UTF-8. Invalid Unicode character values, such as surrogate values, raise a runtime encoding error.

var chars : [3]wchar = ['A', 'é', '€']
var text : str = StrFromWchars(chars[:]) // "Aé€"

2.9.5.9 StrFromUtf16 and StrFromUtf16Ptr

StrFromUtf16(chars : []char16) -> str
StrFromUtf16Ptr(chars : ^char16, count : int) -> str

StrFromUtf16(chars) converts a UTF-16 array slice to UTF-8, ignoring one final zero code unit if present. Internal zeros are preserved. StrFromUtf16Ptr(chars, count) converts exactly count code units, including any zeros in that range. Surrogate pairs represent one Unicode character; malformed UTF-16 raises a runtime encoding error. The pointer form returns empty text for nil or a non-positive count.

var units : [4]char16 = [char16(65), char16(0), char16(66), char16(0)]
var text : str = StrFromUtf16(units[:])          // "A\0B": 3 bytes
var prefix : str = StrFromUtf16Ptr(%units[0], 1) // "A"

2.9.5.10 UTF16 Fallback Conversion

StrFromUtf16Fallback(chars : []char16, fallback_wchar : wchar) -> str
StrFromUtf16PtrFallback(chars : ^char16, count : int, fallback_wchar : wchar) -> str

StrFromUtf16Fallback and StrFromUtf16PtrFallback use the same input bounds as their strict counterparts but replace malformed UTF-16 sequences with fallback_wchar. Passing wchar(0) as the fallback skips malformed sequences.

var units : [3]char16 = [char16(0xD800), char16(65), char16(0)]
var replaced : str = StrFromUtf16Fallback(units[:], '?') // "?A"
var skipped : str = StrFromUtf16PtrFallback(%units[0], 2, wchar(0)) // "A"

2.9.6 String Properties

2.9.6.1 Length

.length counts text bytes, including internal zero bytes when the source has a known length. It excludes the trailing terminator. str and strslice store their lengths; embedded strings and rostr may need to scan for the first zero byte when the length is unknown.

var text : str = "Aé€"
var bytes : int = text.length  // 6 bytes
var empty : str
var zero : int = empty.length // 0, even without an allocated handler

2.9.6.2 Capacity

.capacity is the number of text bytes a str can hold before it needs more storage. It excludes the extra terminating zero byte and can exceed .length. An empty string with no allocated handler has capacity zero.

var text : str = "abc"
text.Reserve(100)
var length : int = text.length     // 3
var has_room : bool = text.capacity >= 100 // true

2.9.6.3 Embedded Storage Size

For embstr(N), .storage_size is N and .maxlength is N - 1. An unsized embstr reports these values for the buffer it references.

var buffer : embstr(8) = "abc"
var size : int = buffer.storage_size // 8
var limit : int = buffer.maxlength   // 7
buffer.Add("defghi")                 // buffer is "abcdefg": truncated to fit

2.9.6.4 Reference Count

.refcount reports how many references share a dynamic string handler. Assigning one str to another shares the handler; modifying one value detaches its storage when necessary. A string with no handler has reference count zero. Temporary values and function arguments can also retain references.

var a : str = "abc"
var b : str = a
var shared : int = a.refcount // 2
b[0] = 'X'                   // a is "abc"; b is "Xbc"
var unique : int = a.refcount // 1

2.9.6.5 Raw Byte Pointer

.pchar borrows a pointer to the first text byte without copying or retaining storage. str, rostr, and embedded strings provide terminated storage; a strslice pointer is not guaranteed to be zero-terminated. Copy a slice to str or embstr(N) before passing it to a C API that expects a C string.

var text : str = "hello"
var pointer : ^char = text.pchar
var first : char = pointer^ // 'h'

var part : strslice = text[:2] // "he", followed by more source bytes
var terminated : str = part
var c_text : ^char = terminated.pchar // points to "he" plus a zero terminator

Keep the source alive and its storage stable while using the pointer. Obtain a new pointer after a mutation that can move the text. For str, .pchar returns terminated empty text even when the internal handler pointer is nil. C string functions see only the prefix before the first zero byte. See C Interoperability.

2.9.7 String Methods

These methods modify str or embedded string storage. Dynamic strings grow as needed and preserve other values that share their storage. Embedded strings keep their fixed capacity and truncate inserted or appended text to fit. An unsized embstr modifies the original buffer through its shared descriptor.

Text-source arguments accept str, rostr, strslice, embstr(N), unsized embstr, or ^char. .Set, .Add, .Append, .Prepend, and .Insert also accept one char byte. A wchar variable must be encoded as UTF-8 to append its text; it is not a single-byte source.

2.9.7.1 .Set

.Set(value : strslice)
.Set(value : char)

.Set(value) replaces the contents. For text values, assignment provides the same replacement syntax. Embedded destinations truncate the source to fit.

var text : str = "old"
text.Set("new")           // "new"
text.Set('!')             // "!"
var buffer : embstr(8)
buffer.Set("abcdefghij")  // "abcdefg"

2.9.7.2 .Add, .Append, and .AppendChar

.Add(value : strslice)
.Add(value : char)
.Append(value : strslice)
.Append(value : char)
.AppendChar(ch : char)

.Add(value) and .Append(value) append text or a char byte. .AppendChar(ch) accepts only a char. These methods modify the string and return no value. An embstr(N) declaration starts empty, so it can be appended to immediately.

var text : str = "abc"
text.Add("def")           // "abcdef"
text.Append('!')          // "abcdef!"
text.AppendChar('?')      // "abcdef!?"

2.9.7.3 .AddFmt

.AddFmt(fmt : strslice, args : varargs)

.AddFmt(fmt, args) appends text containing formatted values. The format parameter is strslice, so substring views can be passed directly. An embstr(N) declaration starts empty and can be used directly with .AddFmt(). Embedded destinations truncate the output to fit. Placeholder syntax and supported value types are explained in Text Formatting.

var text : str = "value="
text.AddFmt("{:04X}", [255]) // "value=00FF"
var buffer : embstr(16)
buffer.AddFmt("{} items", [3]) // "3 items"

2.9.7.4 .Prepend and .Insert

.Prepend(value : strslice)
.Prepend(value : char)
.Insert(index : int, value : strslice)
.Insert(index : int, value : char)

.Prepend(value) inserts at the beginning. .Insert(index, value) inserts before the specified byte position. The insertion position is clamped to 0 .. text.length; $end inserts at the end.

var text : str = "bc"
text.Prepend('a')         // "abc"
text.Insert(1, "--")      // "a--bc"
text.Insert($end, '!')    // "a--bc!"
text.Insert(100, '?')     // "a--bc!?": position clamped to the end

Performance: .Insert() shifts the bytes after the insertion position to make room. Inserting near the beginning of a large string can take significant time. .Prepend() moves all existing text bytes.

2.9.7.5 .Delete

.Delete(index : int, count : int = 1)

.Delete(index, count = 1) removes up to count bytes starting at index. The position is clamped to 0 .. text.length, and the count is limited to the remaining bytes. A non-positive count or a position at the end removes nothing.

var text : str = "abcdef"
text.Delete(1)            // "acdef"
text.Delete(2, 100)       // "ac": removes the entire remaining suffix
text.Delete(100)          // "ac": unchanged

Performance: .Delete() shifts the bytes after the removed range to close the gap. Deleting near the beginning of a large string can take significant time because most of the remaining text must move.

2.9.7.6 .Clear

.Clear(free_storage : bool = false)  // str
.Clear()                           // embstr

.Clear() makes the string empty and retains its storage for reuse. For str, this is equivalent to .Clear(false). .Clear(true) releases that value's reference to the dynamic storage and leaves its length and capacity at zero. Other strings sharing the old storage retain their text. Embedded strings support only the no-argument form.

var text : str = "abc"
text.Reserve(100)
text.Clear()              // empty; capacity is retained
text.Add("reused")
text.Clear(true)          // empty; capacity is 0
var buffer : embstr(16) = "abc"
buffer.Clear()            // empty; the 16-byte buffer remains

2.9.7.7 .SetLength and .Truncate

.SetLength(length : uint, fill : char)
.Truncate(length : uint)

.SetLength(length, fill) sets the byte length, using the supplied char for new bytes when growing. Shortening discards the suffix. .Truncate(length) only shortens: a length greater than the current length leaves the text unchanged. Lengths must be non-negative.

var text : str = "abc"
text.SetLength(5, '.')    // "abc.."
text.SetLength(4, ' ')    // "abc."
text.Truncate(2)          // "ab"
text.Truncate(100)        // "ab": does not grow

2.9.7.8 .Pop and .PopFirst

.Pop() -> char
.Pop(count : int) -> str
.PopFirst() -> char
.PopFirst(count : int) -> str

.Pop() removes and returns the last char; .PopFirst() removes and returns the first. These no-argument forms raise a runtime error on an empty string.

With a count argument, both methods return a str containing the removed bytes. Counts greater than the length remove everything. A non-positive count or an empty string returns empty text.

var text : str = "abcdef"
var tail : str = text.Pop(2)        // "ef"; text is "abcd"
var head : str = text.PopFirst(2)   // "ab"; text is "cd"
var last : char = text.Pop()        // 'd'; text is "c"
var first : char = text.PopFirst()  // 'c'; text is empty
var empty : str = text.Pop(10)      // "": count form accepts an empty string

Performance: Both forms of .PopFirst() shift all remaining text bytes to the beginning of the string. This can take significant time for large strings, especially when repeatedly removing one byte at a time.

2.9.7.9 Overlapping Source and Destination

Dynamic string mutation preserves the logical source when it aliases the destination, including a view into the same string across a reallocation.

var text : str = "abcd"
text.Insert(2, text[1:3]) // "abbccd": inserts the original "bc"
text.Add(text)           // "abbccdabbccd"

2.9.8 Managing Dynamic Storage

2.9.8.1 .Reserve

.Reserve(capacity : uint)

.Reserve(capacity) ensures space for at least the specified number of text bytes without changing the length or contents. It does not reduce existing capacity. Reserving an estimated final size avoids repeated reallocations and copying while assembling text. The runtime doubles capacity as needed when a growing string exceeds its current allocation.

var text : str
text.Reserve(32)
text.Add("Hello, ")
text.AddFmt("{}!", ["world"]) // "Hello, world!"

2.9.8.2 .SetCapacity and .Compact

.SetCapacity(capacity : uint)
.Compact()

.SetCapacity(capacity) sets the capacity to the requested number of text bytes. Reducing it below .length also truncates the contents. .SetCapacity(0) releases the storage. .Compact() sets capacity to the current length. Call these after building text when reducing retained storage is useful.

var text : str = "abcdef"
text.SetCapacity(4)       // "abcd"; capacity is 4
text.Reserve(100)
text.Compact()            // "abcd"; capacity is 4 again

2.9.8.3 .Clone

.Clone() -> str

.Clone() returns an independent copy immediately. Ordinary str assignment shares storage and detaches on mutation; use .Clone() when the copy should already have its own storage before either value changes.

var original : str = "abc"
var copy : str = original.Clone()
copy[0] = 'X'             // copy is "Xbc"; original is "abc"

2.9.9 String Utility Methods

These methods work on every string type. They do not modify the original string. Needles, prefixes, suffixes, trim sets, and padding accept text or a char byte. Searches compare bytes exactly, with case sensitivity and no Unicode normalization. Case conversion, trimming, and padding return a new str; assign the result to keep it.

2.9.9.1 .Upper and .Lower

.Upper() -> str
.Lower() -> str

.Upper() and .Lower() return a copy with ASCII letters converted to uppercase or lowercase. They preserve non-ASCII bytes, including UTF-8 characters, and do not modify the original string. They are equivalent to StrUpper(text) and StrLower(text) and require dynamic-string support.

var text : embstr(32) = "Hello, é!"
var upper : str = text.Upper() // "HELLO, é!"
var lower : str = text.Lower() // "hello, é!"; text is still "Hello, é!"

2.9.9.2 .ToInt and .ToFloat

.ToInt() -> int
.ToInt(defvalue : int) -> int
.ToFloat() -> float
.ToFloat(defvalue : float) -> float

These methods parse a number using the same rules as StrToInt and StrToFloat. Without a fallback, invalid or empty text raises EStrError; with defvalue, conversion errors return that value. Integer conversion also checks the range of int. Neither method skips surrounding whitespace. Both work without dynamic-string support.

var integer : embstr(16) = "-42"
var count : int = integer.ToInt()        // -42
var decimal : rostr = "1,25e2"
var value : float = decimal.ToFloat()    // 125
var invalid : strslice = "invalid"
var fallback : int = invalid.ToInt(-1)   // -1
var default_value : float = invalid.ToFloat(0) // 0
var padded : rostr = " 42 "
var trimmed : int = padded.Trim().ToInt() // 42

2.9.9.3 .IndexOf and .LastIndexOf

.IndexOf(needle : strslice, start : int = 0) -> int
.IndexOf(needle : char, start : int = 0) -> int
.LastIndexOf(needle : strslice) -> int
.LastIndexOf(needle : char) -> int

.IndexOf(needle, start = 0) returns the first matching byte offset at or after start. .LastIndexOf(needle) searches for the final occurrence in the entire text. Both return -1 when no match exists.

.IndexOf clamps start to 0 .. text.length. An empty needle matches at the normalized start position; .LastIndexOf("") returns text.length.

var text : rostr = "abcdefabc"
var first : int = text.IndexOf("abc")      // 0
var next : int = text.IndexOf("abc", 1)    // 6
var last : int = text.LastIndexOf("abc")   // 6
var byte : int = text.IndexOf('d')         // 3
var missing : int = text.IndexOf("xyz")    // -1
var end : int = text.IndexOf("", 100)      // 9

2.9.9.4 .Contains, .StartsWith, and .EndsWith

.Contains(needle : strslice) -> bool
.Contains(needle : char) -> bool
.StartsWith(prefix : strslice) -> bool
.StartsWith(prefix : char) -> bool
.EndsWith(suffix : strslice) -> bool
.EndsWith(suffix : char) -> bool

.Contains(needle) tests for a match anywhere. .StartsWith(prefix) and .EndsWith(suffix) test only the corresponding end. An empty needle, prefix, or suffix matches every text value, including empty text.

var path : embstr(32) = "docs/reference.md"
var found : bool = path.Contains("reference") // true
var prefix : bool = path.StartsWith("docs/")  // true
var suffix : bool = path.EndsWith(".md")      // true
var case_match : bool = path.EndsWith(".MD")  // false

2.9.9.5 .Trim, .LTrim, and .RTrim

.Trim() -> str
.Trim(set : strslice) -> str
.Trim(set : char) -> str
.LTrim() -> str
.LTrim(set : strslice) -> str
.LTrim(set : char) -> str
.RTrim() -> str
.RTrim(set : strslice) -> str
.RTrim(set : char) -> str

.Trim() removes ASCII whitespace from both ends; .LTrim() removes it only from the left, and .RTrim() only from the right. Default whitespace is space, tab, line feed, carriage return, vertical tab, and form feed.

An explicit argument is a set of bytes, not a substring pattern. Bytes in that set are removed repeatedly from the selected ends. Interior bytes remain.

var text : strslice = "  abc  "
var both : str = text.Trim()       // "abc"
var left : str = text.LTrim()      // "abc  "
var right : str = text.RTrim()     // "  abc"
var marked : rostr = "-=-abc-=-"
var name : str = marked.Trim("-=") // "abc"
var dashes : str = "---abc---"
dashes = dashes.Trim('-')          // "abc": assign to replace the original

2.9.9.6 .LPad and .RPad

.LPad(length : int, fill : strslice) -> str
.LPad(length : int, fill : char) -> str
.RPad(length : int, fill : strslice) -> str
.RPad(length : int, fill : char) -> str

.LPad(length, fill) and .RPad(length, fill) return text padded to the requested byte length. They repeat the fill value and truncate the last repetition as needed. If the requested length is no greater than the source length, the result has the original contents. An empty fill value raises a runtime error. Padding operates on bytes and can split a multibyte UTF-8 fill value.

var code : rostr = "abc"
var left : str = code.LPad(8, "01") // "01010abc"
var right : str = code.RPad(5, '.') // "abc.."
var same : str = code.LPad(2, ' ')  // "abc": no truncation

2.9.10 Byte Indexing and Slicing

text[index] reads one char byte from any string type. str and embedded strings also permit replacing a byte; rostr and strslice are read-only. Indices are zero-based. An index outside the existing bytes raises a runtime range error. Negative indices do not count backward from the end; use text[$last] or text[$end - 1] for the last byte. These operations do not decode or validate UTF-8.

text[start:end] borrows a half-open byte range as strslice, while text[start::end] includes the end byte. Slicing is supported on every string type: str, rostr, strslice, embstr(N), and unsized embstr. Slice bounds are clamped to [0, length]; reversed normalized bounds produce an empty slice.

$last is the last existing byte index and $end is the one-past-end position for the string being indexed or sliced. Omitting a slice bound selects the beginning or end. Negative slice bounds are clamped, rather than counted from the end; use $end - count to select a suffix.

var text : str = "abcdef"
var first : char = text[0]             // 'a'
text[0] = 'A'                          // text is "Abcdef"
var last : char = text[$last]          // 'f'
var part : strslice = text[1:3]        // "bc": end byte excluded
var inclusive : strslice = text[1::3]  // "bcd": end byte included
var head : strslice = text[:2]         // "Ab"
var tail : strslice = text[$end - 2:]  // "ef"
var all : strslice = text[-5:100]      // "Abcdef": bounds clamped
var empty : strslice = text[4:2]       // ""
var buffer : embstr(16) = "abcdef"
var from_buffer : strslice = buffer[1:4] // "bcd": borrows embedded storage

Slices borrow the source bytes. Keep the owner alive and its storage and contents stable while using them. Assign a slice to str to make an owned copy.

2.9.11 Unicode Operations

The following members decode UTF-8 and report a runtime encoding error for malformed input. A displayed symbol can combine multiple Unicode characters, for example a letter and a combining accent. .wclen and .wchar[...] are available on every string type, including embstr(N) and unsized embstr.

2.9.11.1 Unicode Character Count

.wclen counts decoded Unicode characters. Unlike .length, it requires UTF-8 decoding rather than reading a stored byte count.

var text : embstr(16) = "Aé€"
var bytes : int = text.length // 6
var characters : int = text.wclen // 3

2.9.11.2 Unicode Indexing and Slicing

.wchar[index] reads one Unicode character as wchar. .wchar[start:end] returns a decoded range as [*]wchar; the start::end form includes the final character. In this context, $last and $end refer to Unicode character positions. Slice bounds are clamped; an invalid character index raises a runtime range error.

var text : embstr(16) = "Aé€"
var second : wchar = text.wchar[1]          // 'é'
var chars : [*]wchar = text.wchar[1:$end]   // ['é', '€']
var part : str = StrFromWchars(chars[:])   // "é€": encode the selected characters

2.9.11.3 .ToWchars

.ToWchars() -> [*]wchar

.ToWchars() decodes the whole string into [*]wchar. It works on every string type, including strslice, embstr(N), and unsized embstr. For repeated indexed Unicode processing, convert once and index the resulting array to avoid decoding the UTF-8 prefix for every lookup.

var text : embstr(16) = "Aé€"
var chars : [*]wchar = text.ToWchars()
var second : wchar = chars[1] // 'é'
var view : strslice = text
var from_view : [*]wchar = view.ToWchars() // ['A', 'é', '€']