Strings helpers
In the terminalela --spec strings-helpers
Modelled on Go's strings package, under the str prefix.
| Built-in | Does |
|---|---|
str.split(s, sep) | Returns a list |
str.join(list, sep) | Returns a string |
str.replace(s, old, new) | Replaces all occurrences |
str.trim(s) | Removes surrounding whitespace |
str.fields(s) | The words of s between runs of whitespace, as a list; empty for a blank s |
str.chars(s) | The characters of s as a list, each a string of one character, counted as $s[i] counts them |
str.lines(s) | The lines of s as a list, each without its \n; a \n at the end ends the last line and starts no empty one, and a \r stays |
str.head(s, n) / str.tail(s, n) | The first or last n characters of s, all of s when it is shorter (First and last items) |
str.cut(s, sep) | Gives three values, the text before the first sep, the text after it, and whether sep was found; not found gives s, "", false |
str.trim_prefix(s, prefix) / str.trim_suffix(s, suffix) | Removes prefix from the start or suffix from the end, once, when it is there |
str.trim_left(s, chars) / str.trim_right(s, chars) | Removes every character in the set chars from the start or the end |
str.contains(s, sub) | Returns a boolean |
str.index(s, sub) | The position of the first sub in s, counting from 0 as $s[i] does; a sub that is not there is an error, so test with str.contains first |
str.last_index(s, sub) | The position of the last sub in s, counted as str.index counts; a sub that is not there is an error |
str.has_prefix(s, prefix) | Returns a boolean |
str.has_suffix(s, suffix) | Returns a boolean |
str.upper(s) / str.lower(s) | Changes case |
str.match(glob, s) | Whether the whole of s matches the glob, by the rules of Paths and directories: str.match("*.[ch]", $name) |
key, value, ok = str.cut($line, "=")words = str.fields(" a b\tc ") # ["a", "b", "c"]hashes = 0 # the # characters in notes.txtfor line in str.lines(file.read("notes.txt")) for c in str.chars($line) if $c == "#" hashes = $hashes + 1 end if end forend forname = str.trim_suffix("deploy.ela", ".ela") # "deploy"version = str.trim_left("v0.1.0", "v")if str.contains($line, "=") value = $line[str.index($line, "=") + 1:]end ifif str.contains($url, "/") page = $url[str.last_index($url, "/") + 1:]end if