strings
lhj
summon stringsCase
lhj
strings.upper("hello") ## "HELLO"
strings.lower("WORLD") ## "world"
strings.title("hello world") ## "Hello World"
strings.capitalize("hello") ## "Hello"Whitespace
lhj
strings.strip(" hi ") ## "hi"
strings.lstrip(" hi ") ## "hi "
strings.rstrip(" hi ") ## " hi"Split and join
lhj
strings.split("a,b,c", ",") ## ["a", "b", "c"]
strings.split("hello world") ## ["hello", "world"]
strings.join(["x", "y", "z"], "-") ## "x-y-z"
strings.join(["a", "b", "c"]) ## "abc" (no separator)Search and replace
lhj
strings.contains("hello world", "world") ## yep
strings.starts_with("hello", "hel") ## yep
strings.ends_with("hello", "llo") ## yep
strings.find("hello", "ll") ## 2 (index or -1)
strings.count("banana", "a") ## 3
strings.replace("foo bar foo", "foo", "baz") ## "baz bar baz"
strings.replace_first("aaa", "a", "b") ## "baa"Padding and alignment
lhj
strings.pad_left("42", 5) ## " 42"
strings.pad_right("42", 5) ## "42 "
strings.pad_left("42", 5, "0") ## "00042"
strings.center("hi", 10) ## " hi "Checking
lhj
strings.is_digit("123") ## yep
strings.is_alpha("abc") ## yep
strings.is_alnum("abc123") ## yep
strings.is_space(" ") ## yep
strings.is_empty("") ## yep
strings.is_empty(" ") ## nopeSlicing and indexing
Standard list-style slicing works on strings:
lhj
forge s = "hello"
echo s[0] ## "h"
echo s[1:3] ## "el"
echo s[-1] ## "o"
echo s[::-1] ## "olleh" (reverse)Conversion
lhj
strings.to_int("42") ## 42
strings.to_float("3.14") ## 3.14
strings.to_list("abc") ## ["a", "b", "c"]
strings.from_chars(["h","i"]) ## "hi"Example
lhj
summon strings
forge csv = "Alice,30,Engineer"
forge parts = strings.split(csv, ",")
forge name = strings.strip(parts[0])
forge age = strings.to_int(parts[1])
forge role = strings.lower(parts[2])
echo "{name} is a {age}-year-old {role}"
## Alice is a 30-year-old engineer