Standard library
std/strings
import "std/strings"
for line in lines("name;score\nada;9\nlin;12") {
var parts = split(line, ";")
print(padRight(parts[0], 6, "."), padLeft(parts[1], 5, " "))
}Every function also works as a method on its first argument: "7".padLeft(3, "0"), text.lines().
The built-in string functions need no import: len, substring, charAt, split, trim, upper, lower,
startsWith, endsWith, contains, indexOf, replace, repeat, chars, charCode, fromCharCode, bytes,
fromBytes, toString, toInt, toFloat, isInt and isNumber (built-in functions).
Importing std/strings also gives the character classes isLetter, isDigit, isAlphanumeric and isWhitespace,
which test whether every character of a non-empty text is a letter (in any script), a digit 0 to 9, a letter or
number, or whitespace.
Functions
| Function | Result | Example |
|---|---|---|
padLeft(text, width, fill) |
fill added in front until the text has width characters |
padLeft("42", 5, "0") is "00042" |
padRight(text, width, fill) |
fill added at the end until the text has width characters |
padRight("ab", 5, ".") is "ab..." |
isEmpty(text) |
whether the text has no characters | isEmpty(" ") is false |
isBlank(text) |
whether nothing is left after trimming whitespace | isBlank(" \t") is true |
capitalize(text) |
the first character in uppercase | capitalize("hello world") is "Hello world" |
countOf(text, part) |
how often part occurs, without overlaps |
countOf("banana", "an") is 2 |
lines(text) |
the lines, split at \n or \r\n |
lines("a\r\nb\nc") is ["a", "b", "c"] |
words(text) |
the words between spaces, without empty ones | words(" the quick fox ") is ["the", "quick", "fox"] |
isDigits(text) |
whether the text is non-empty and only 0-9 |
isDigits("-1") is false |
isAlpha(text) |
whether the text is non-empty and only ASCII letters | isAlpha("Burn2") is false |
Details
- Lengths and positions count characters (Unicode code points), not bytes:
len("héllo")is5.bytes(text)gives the UTF-8 bytes as[uint8]. - Text that is already long enough is returned unchanged by
padLeftandpadRight; a fill of more than one character can make the result longer thanwidth, and an empty fill changes nothing. - A line break at the end gives
linesan empty last line.wordsonly splits at spaces; split at other characters withsplit. - Strings are values: functions return new strings and never change the one they get.
s += "x"appends in place whensis the only reference to the string, so building a string in a loop is fast. - String templates format any value:
"${name} has ${len(items)} items".