strings

The strings module collects everything you need to inspect, transform, and assemble text — searching, slicing, splitting, joining, trimming, case conversion, padding, classification, formatting, and an efficient string builder. Import it with import strings.

Searching

FunctionSignatureDescription
indexOfstrings.indexOf(s: string, sub: string) → intFirst index of sub, or -1 if absent
lastIndexOfstrings.lastIndexOf(s: string, sub: string) → intLast index of sub, or -1 if absent
containsstrings.contains(s: string, sub: string) → boolWhether s contains sub
countstrings.count(s: string, sub: string) → intNon-overlapping occurrences of sub
hasPrefixstrings.hasPrefix(s: string, prefix: string) → boolWhether s starts with prefix
hasSuffixstrings.hasSuffix(s: string, suffix: string) → boolWhether s ends with suffix

Slicing

FunctionSignatureDescription
slicestrings.slice(s: string, start: int, end: int) → stringByte slice from start to end; negative indices count from the end
substrings.sub(s: string, start: int, length: int) → stringSubstring of length bytes from start; negative start counts from the end

Splitting

FunctionSignatureDescription
splitstrings.split(s: string, sep: string) → string[]Split on every occurrence of sep
splitNstrings.splitN(s: string, sep: string, n: int) → string[]Split into at most n parts; -1 means unlimited
fieldsstrings.fields(s: string) → string[]Split on any whitespace, discarding empty parts
linesstrings.lines(s: string) → string[]Split on newlines, stripping trailing carriage returns

Joining

FunctionSignatureDescription
joinstrings.join(arr: string[], sep: string) → stringJoin array elements with sep

Replacement

FunctionSignatureDescription
replacestrings.replace(s: string, old: string, new: string) → stringReplace all occurrences of old
replaceNstrings.replaceN(s: string, old: string, new: string, n: int) → stringReplace first n occurrences; -1 replaces all

Trimming

FunctionSignatureDescription
trimstrings.trim(s: string, cutset?: string) → stringTrim whitespace, or the given cutset, from both ends
trimLeftstrings.trimLeft(s: string, cutset: string) → stringTrim cutset chars from the left
trimRightstrings.trimRight(s: string, cutset: string) → stringTrim cutset chars from the right
trimPrefixstrings.trimPrefix(s: string, prefix: string) → stringRemove prefix if present
trimSuffixstrings.trimSuffix(s: string, suffix: string) → stringRemove suffix if present

Case

FunctionSignatureDescription
upperstrings.upper(s: string) → stringConvert to uppercase
lowerstrings.lower(s: string) → stringConvert to lowercase
titlestrings.title(s: string) → stringCapitalize the first letter of each word

Padding and repetition

FunctionSignatureDescription
padLeftstrings.padLeft(s: string, width: int, pad: string) → stringRight-align in a field of width bytes
padRightstrings.padRight(s: string, width: int, pad: string) → stringLeft-align in a field of width bytes
repeatstrings.repeat(s: string, n: int) → stringRepeat s n times
reversestrings.reverse(s: string) → stringReverse the rune order

Characters and codepoints

FunctionSignatureDescription
charstrings.char(code: int) → stringUTF-8 string for the given codepoint
codestrings.code(s: string) → intCodepoint of the first rune, or -1 if empty
byteAtstrings.byteAt(s: string, i: int) → intRaw byte 0-255 at byte index i, or -1 if out of range
fromBytesstrings.fromBytes(arr: int[]) → stringBuild a string from byte values
toBytesstrings.toBytes(s: string) → int[]Array of byte values 0-255
runeLenstrings.runeLen(s: string) → intNumber of Unicode runes, not bytes

Classification

FunctionSignatureDescription
isAlphastrings.isAlpha(s: string) → boolAll chars are Unicode letters
isDigitstrings.isDigit(s: string) → boolAll chars are ASCII digits 0-9
isAlphaNumstrings.isAlphaNum(s: string) → boolAll chars are letters or digits
isSpacestrings.isSpace(s: string) → boolAll chars are whitespace

Formatting and conversion

FunctionSignatureDescription
formatstrings.format(template: string, args: string[]) → stringReplace {0}, {1}, … with the matching args
toIntstrings.toInt(s: string) → intParse a decimal integer, 0 on failure
toFloatstrings.toFloat(s: string) → floatParse a float, 0.0 on failure
matchGlobstrings.matchGlob(pattern: string, s: string) → boolMatch a glob pattern with * and ?

strings.builder

Building strings in a loop with out = out + chunk is O(n squared). The builder accumulates into one buffer for O(n) growth, then materializes the result once at the end.

FunctionSignatureDescription
newstrings.builder.new() → intAllocate a builder, returns its handle
writestrings.builder.write(id: int, s: any) → boolAppend a value; false if the handle is unknown
writelnstrings.builder.writeln(id: int, s: any) → boolAppend a value followed by a newline
toStringstrings.builder.toString(id: int) → stringMaterialize the accumulated string
lenstrings.builder.len(id: int) → intNumber of bytes accumulated
resetstrings.builder.reset(id: int) → voidClear the buffer, keep the handle
freestrings.builder.free(id: int) → boolRelease the handle; true if it existed

Example

import strings

let parts = strings.split("a,b,c", ",")
let joined = strings.join(parts, " | ")
print(strings.upper(joined))            // A | B | C

let b = strings.builder.new()
strings.builder.write(b, "count=")
strings.builder.write(b, "42")
print(strings.builder.toString(b))      // count=42
strings.builder.free(b)
Standard library · View as Markdown · llms-full.txt