Skip to content

std.strings

import { Aside } from ‘@astrojs/starlight/components’;

std.strings provides byte-oriented operations on string values: searching, trimming, case conversion, replacement, splitting and joining. It is the text-layer counterpart to std.bytes, which provides the same API over mutable []byte.

Every function that produces a string allocates a fresh buffer — Sysl strings are immutable and there is no zero-copy substring view today. Operations that don’t change the string (like contains) don’t allocate.

import std.strings.*
FunctionSignatureBehaviour
has_prefix(s, prefix)(string, string) -> boolEmpty string is a prefix of every string
has_suffix(s, suffix)(string, string) -> boolEmpty string is a suffix of every string
index(s, sub)(string, string) -> intByte offset of first occurrence, or -1
last_index(s, sub)(string, string) -> intByte offset of last occurrence, or -1
contains(s, sub)(string, string) -> boolConvenience around index >= 0
count(s, sub)(string, string) -> intNon-overlapping occurrences; empty sub returns len(s) + 1 (Go parity)
index_byte(s, b)(string, byte) -> intFirst byte offset where s[i] == b
last_index_byte(s, b)(string, byte) -> intLast byte offset where s[i] == b
FunctionSignatureBehaviour
substring(s, start, end)(string, int, int) -> stringHalf-open range [start, end), traps on out-of-bounds
FunctionSignatureBehaviour
trim_space(s)(string) -> stringStrip ASCII whitespace from both ends
trim_left(s, cutset)(string, string) -> stringStrip any leading byte that appears in cutset
trim_right(s, cutset)(string, string) -> stringStrip any trailing byte that appears in cutset
trim(s, cutset)(string, string) -> stringtrim_left then trim_right
FunctionSignature
to_upper(s)(string) -> string
to_lower(s)(string) -> string
FunctionSignatureBehaviour
replace_all(s, old, new)(string, string, string) -> stringEvery non-overlapping occurrence
repeat(s, count)(string, int) -> stringTraps on negative count
FunctionSignatureBehaviour
split(s, sep)(string, string) -> []stringSplits on every occurrence of sep
fields(s)(string) -> []stringSplits on runs of ASCII whitespace
join(xs, sep)([]string, string) -> stringJoins slice with sep between elements
import std.strings.*
main() -> int
val line = " user=alice role=admin "
val kvs = fields(trim_space(line))
for kv in kvs
val eq = index_byte(kv, '=')
if eq >= 0
val k = substring(kv, 0, eq)
val v = substring(kv, eq + 1, len(kv))
puts(s"$k -> $v")
0

Output:

user -> alice
role -> admin
  • std.builder — growable string builder used to assemble result strings inside the module.
  • std.strconv — parse integers, format numbers, quote strings.
  • std.regex — when you need real pattern matching instead of substring search.