std/str
String helpers. Import with import "std/str";. Available everywhere.
A string is an array of char (bytes), so these functions work byte-wise.
Case conversion covers ASCII letters only. For length, indexing, and slicing
use the built-in array methods (s.len(), s[i], s.slice(a, b)).
Searching
Section titled “Searching”index_of
Section titled “index_of”int? index_of(haystack: string, needle: string)Returns the index of the first occurrence of needle in haystack, or none
if it does not occur. An empty needle matches at index 0.
str.index_of("hello world", "world") # 6str.index_of("hello", "xyz") # nonecontains
Section titled “contains”bool contains(haystack: string, needle: string)Returns true if needle occurs anywhere in haystack.
str.contains("hello world", "lo w") # truestarts_with
Section titled “starts_with”bool starts_with(s: string, prefix: string)Returns true if s begins with prefix.
ends_with
Section titled “ends_with”bool ends_with(s: string, suffix: string)Returns true if s ends with suffix.
str.starts_with("main.kora", "main") # truestr.ends_with("main.kora", ".kora") # trueTransforming
Section titled “Transforming”to_upper
Section titled “to_upper”string to_upper(s: string)Returns a copy of s with ASCII letters a to z uppercased.
to_lower
Section titled “to_lower”string to_lower(s: string)Returns a copy of s with ASCII letters A to Z lowercased.
str.to_upper("kora 1.0") # "KORA 1.0"str.to_lower("KoRa") # "kora"string trim(s: string)Returns s without leading and trailing whitespace (spaces, tabs, newlines,
carriage returns).
str.trim(" hi \n") # "hi"repeat
Section titled “repeat”string repeat(s: string, n: int)Returns s concatenated n times. Returns "" when n is zero or negative.
str.repeat("ab", 3) # "ababab"reverse
Section titled “reverse”string reverse(s: string)Returns s with its bytes in reverse order.
str.reverse("kora") # "arok"Splitting and joining
Section titled “Splitting and joining”[string] split(s: string, sep: char)Splits s at every occurrence of sep. The separator is not included in the
pieces. n separators always produce n + 1 pieces, so adjacent separators
yield empty strings.
str.split("a,b,c", ',') # ["a", "b", "c"]str.split("a,,c", ',') # ["a", "", "c"]str.split("abc", ',') # ["abc"]string join(parts: [string], sep: string)Concatenates parts with sep between consecutive elements.
str.join(["usr", "local", "bin"], "/") # "usr/local/bin"Classifying
Section titled “Classifying”is_space
Section titled “is_space”bool is_space(c: char)Returns true if c is a space, tab, newline, or carriage return.
str.is_space(' ') # truestr.is_space('x') # false