Standard Library: Str

Work with strings (pieces of text)

For more information on strings see the language reference.


Module Contents


Str.chars(string)

Split string into a list of single-character strings.

chars("Hello World!")
chars("🧑🏽")
["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d", "!"]
["🧑", "🏽"]

Str.drop(string, count)

Remove the first count characters from string. If count >= len(string) then "" is returned.

drop("Hello World!", 5)
" World!"

(Accessible as a global through Overloads.drop)


Str.dropLast(string, count)

Remove the last count characters from string. If count >= len(string) then "" is returned.

Str.dropLast("Hello World!", 5)
"Hello W"

Str.endsWith(string, prefix)

Check whether string ends with prefix.

Str.endsWith("apple pie", "pie")
Str.endsWith("apple pie", "cake")
true
false

Str.get(string, index)

Get the character at position index in string.

Str.get("Hello World!", 1)
"e"

Str.has(string, subString)

Check whether string contains the substring subString.

Str.has("assume", "sum")
Str.has("assume", "you")
true
false

Note that this can also be accomplished using the in operator.

"sum" in "assume"
"you" in "assume"
true
false

Str.indexOf(string, subString)

Get the index of the first occurrence of subString in string. Returns none if string does not contain subString.

Str.indexOf("mississippi", "issi")
Str.indexOf("mississippi", "bama")
1
none

Str.isAlpha(string)

Check if string contains only Unicode letters.

Str.isAlpha("hello")
Str.isAlpha("你好")
Str.isAlpha("hi!")
true
true
false

Str.isAlphaNum(string)

Check if string contains only Unicode letters or digits.

Str.isAlphaNum("hello456")
Str.isAlphaNum("你好٤٥٦")
Str.isAlphaNum("hi!")
true
true
false

Str.isAsciiAlpha(string)

Check if string contains only ASCII letters.

Str.isAsciiAlpha("hello")
Str.isAsciiAlpha("你好")
Str.isAsciiAlpha("hi!")
true
false
false

Str.isAsciiAlphaNum(string)

Check if string contains only ASCII letters or digits.

Str.isAsciiAlphaNum("hello456")
Str.isAsciiAlphaNum("你好٤٥٦")
Str.isAsciiAlphaNum("hi!")
true
false
false

Str.isAsciiDigit(string)

Check if string contains only ASCII digits.

Str.isAsciiDigit("456")
Str.isAsciiDigit("٤٥٦")
Str.isAsciiDigit("hello")
true
false
false

Str.isDigit(string)

Check if string contains only Unicode digits.

Str.isDigit("456")
Str.isDigit("٤٥٦")
Str.isDigit("hello")
true
true
false

Str.isEmpty(string)

Check if string is empty (if its length is zero).

isEmpty("")
isEmpty("asdf")
true
false

(Accessible as a global through Overloads.isEmpty)


Str.isLower(string)

Check whether Str.toLower(string) == string.

Str.isLower("hello world!")
Str.isLower("Hello World!")
true
false

Str.isUpper(string)

Check whether Str.toUpper(string) == string.

Str.isUpper("HELLO WORLD!")
Str.isUpper("Hello World!")
true
false

Str.join(list, separator)

Join the values in list into a single string, inserting separator between them. Each value is converted to a string using Str.of.

join([1, 2, 3, 4], " -> ")
join(["Hello", " ", "World", "!"], "")
"1 -> 2 -> 3 -> 4"
"Hello World!"

Str.len(string)

Get the length of string in characters.

len("Hello World!")
len("🧑🏽")
12
2

(Accessible as a global through Overloads.len)


Str.lines(string)

Return a list of the lines in string, where lines are separeted by the regex \r?\n. Separators are not inclued in the returned lines.

Str.lines("foo\n\nbar\r\nbaz\n")
["foo", "", "bar", "baz", ""]

Str.of(value)

Get the string representation of value.

Str.of([100, false, "foo", print])
"[100, false, \"foo\", Console.print(value)]"

Str.padLeft(value, n)

Convert value to a string and pad it on the left with spaces so that the total length is at least n characters.

Str.padLeft("Java", 10)
Str.padLeft("JavaScript", 10)
"      Java"
"JavaScript"

Str.padRight(value, n)

Convert value to a string and pad it on the right with spaces so that the total length is at least n characters.

Str.padRight("Java", 10)
Str.padRight("JavaScript", 10)
"Java      "
"JavaScript"

Str.parse(string)

Convert string to a number, boolean, or none.

parse("45.67")
parse("false")
parse("none")
45.67
false
none

Str.repeat(string, count)

Repeat string count times. If count <= 0, then "" is returned.

Str.repeat("la", 5)
"lalalalala"

_Note that this can also be accomplished using the _ operator*.

"la" * 5
"lalalalala"

Str.replace(string, subString, replacement)

Replace all occurrences of subString in string with replacement.

Str.replace("A catalog of cats", "cat", "dog")
"A dogalog of dogs"

Str.replaceN(string, subString, replacement, count)

Replace the first count occurrence of subString in string with replacement.

Str.replaceN("mississippi", "i", "I", 2)
"mIssissippi"

Str.reverse(string)

Reverse the characters in string.

reverse("Hello World!")
"!dlroW olleH"

(Accessible as a global through Overloads.reverse)


Str.splice(string, index, count, subString)

Remove count characters from string starting at index, and replace them with the string subString.

Str.splice("abcdef", 3, 2, "xyz")
"abcxyzf"

Str.split(string, separator)

Split string into a list of substrings using separator as the delimiter. separator is not included in the resulting substrings.

split("a,b,,c", ",")
["a", "b", "", "c"]

Str.startsWith(string, prefix)

Check whether string begins with prefix.

Str.startsWith("apple pie", "app")
Str.startsWith("apple pie", "pecan")
true
false

Str.take(string, count)

Get a string containing the first count characters from string. If count >= len(string) then the entire string is returned.

take("Hello World!", 5)
"Hello"

(Accessible as a global through Overloads.take)


Str.takeLast(string, count)

Get a string containing the last count characters from string. If count >= len(string) then the entire string is returned.

Str.takeLast("Hello World!", 5)
"orld!"

Str.toLower(string)

Convert string to lowercase.

Str.toLower("Hello World!")
"hello world!"

Str.toUpper(string)

Convert string to uppercase.

Str.toUpper("Hello World!")
"HELLO WORLD!"

Str.trim(string)

Remove the leading and trailing whitespace from string.

Str.trim("  hello\n")
"hello"

Str.trimLeft(string)

Remove the leading whitespace from string.

Str.trimLeft("  hello\n")
"hello\n"

Str.trimRight(string)

Remove the trailing whitespace from string.

Str.trimRight("  hello\n")
"  hello"