std.string — String utilities
Implemented — importable on both engines, byte-identical.
std.string provides string manipulation utilities built on Karkain’s
native string support. Karkain strings are UTF-8 byte strings: len()
returns the byte length and s[i] returns the single byte at i as a
1-character string. The ASCII case-mapping helpers operate byte-wise
(table-based, no character arithmetic) and therefore work identically on both
engines.
All functions are implemented in pure Karkain with untyped parameters and are
identical on the Go and kcc engines.
Importing
import std.string
Module reference
str_len(s)Returns the length (number of bytes) of
s. Delegates tolen(). Returns:intstr_empty(s)Returns
trueifshas zero length. Returns:boolstr_concat(a, b)Returns the concatenation of
aandb. Uses the+operator. Returns:stringstr_repeat(s, n)Returns
srepeatedntimes ("ab"× 3 →"ababab"). An empty string orn <= 0yields"". Returns:stringstr_starts_with(s, prefix)Returns
trueifsstarts withprefix. Returns:boolstr_ends_with(s, suffix)Returns
trueifsends withsuffix. Returns:boolstr_contains(s, sub)Returns
trueifscontainssub. Delegates tocontains(). Returns:boolstr_index_of(s, sub)Returns the byte index of the first occurrence of
subins, or-1if not found. An emptysubmatches at index0. Returns:intstr_last_index_of(s, sub)Returns the byte index of the last occurrence of
subins, or-1if not found. An emptysubreturnslen(s). Returns:intstr_sub(s, start, end)Returns the substring from byte index
startup to (but not including)end, i.e.s[start:end]. Returns:stringstr_slice(s, start, length)Returns the substring starting at byte index
startwith the givenlength. Delegates tosubstr(). Returns:stringstr_trim(s)Removes leading and trailing whitespace. Delegates to
trim(). Returns:stringstr_split(s, delimiter)Splits
sinto an array of strings ondelimiter. Delegates tosplit(). Returns:string[]str_to_upper(s)Converts ASCII lowercase letters to uppercase. Non-ASCII and non-letter bytes are unchanged. Returns:
stringstr_to_lower(s)Converts ASCII uppercase letters to lowercase. Non-ASCII and non-letter bytes are unchanged. Returns:
stringstr_replace(s, old, new)Returns a new string with all occurrences of
oldreplaced bynew. Returns:stringstr_reverse(s)Returns a new string with the bytes of
sin reverse order. Returns:stringstr_char_at(s, index)Returns the single character at byte index
indexas a 1-character string, or""ifindexis out of range. Returns:stringstr_to_int(s)Converts a string to an integer using the builtin
int(). Raises a runtime error on invalid input. Returns:intstr_from_int(n)Converts an integer to its decimal string representation using the builtin
str(). Returns:stringstr_from_float(f)Converts a float to its string representation using the builtin
str(). Returns:stringstr_is_empty(s)Returns
trueifshas zero length (alias ofstr_empty). Returns:boolstr_count(s, sub)Returns the number of non-overlapping occurrences of
subins. An emptysubreturns0. Returns:intstr_join(arr, sep)Joins the elements of the string array
arrwith separatorsepbetween them. Returns:stringstr_trim_left(s)Removes leading whitespace (space, tab, newline, carriage return). Returns:
stringstr_trim_right(s)Removes trailing whitespace (space, tab, newline, carriage return). Returns:
string
Example
import std.string
func main() {
let name = "karkain"
println(str_to_upper(name)) // KARKAIN
println(str_concat("hello", " world")) // hello world
let parts = str_split("a,b,c", ",")
println(len(parts)) // 3
println(str_join(parts, "-")) // a-b-c
println(str_repeat("ab", 3)) // ababab
}
$ karkain run example.kark
KARKAIN
hello world
3
a-b-c
ababab
See also
strings — the language’s built-in string support.