Skip to content

String Prototypes

String values expose text helpers.

let s = "hello world"
println(s.length())
println(s.includes("world"))
println(s.toUpperCase())

Prototype calls currently work reliably on values stored in variables.

let s = "string"
println(s.length())

Calling prototype methods directly on literals is currently limited by AST/parser behavior.

// Currently may not work:
println("string".length())

Prefer assigning the literal to a variable first.

let text = "string"
println(text.length())
MethodReturnsDescription
length()intReturns the byte length of the string.
includes(substr: string)boolChecks whether the string contains substr.
indexOf(substr: string)intReturns the first index of substr, or -1.
lastIndexOf(substr: string)intReturns the last index of substr, or -1.
startsWith(prefix: string)boolChecks whether the string starts with prefix.
endsWith(suffix: string)boolChecks whether the string ends with suffix.
toUpperCase()stringConverts the string to uppercase.
toLowerCase()stringConverts the string to lowercase.
capitalize()stringConverts the string to title case.
trim()stringRemoves whitespace from both ends.
trimPrefix(prefix: string)stringRemoves prefix from the beginning if present.
trimSuffix(suffix: string)stringRemoves suffix from the end if present.
replace(old: string, new: string, n: int = -1)stringReplaces occurrences of old with new. -1 means replace all.
split(sep: string = " ")[]stringSplits the string by sep.
substring(start: int, end: int)stringReturns a substring.
charAt(index: int)charReturns the character at index.
codePointAt(index: int)intReturns the Unicode code point at index.
toKebabCase()stringConverts text to kebab-case.
toCamelCase()stringConverts text to lower camelCase.
toSnakeCase()stringConverts text to snake_case.
let name = "nubo language"
println(name.length())
println(name.includes("language"))
println(name.indexOf("language"))
println(name.startsWith("nubo"))
println(name.endsWith("language"))
let text = " Hello Nubo "
println(text.trim())
println(text.toLowerCase())
println(text.toUpperCase())
println(text.capitalize())
let title = "Nubo Language"
println(title.toKebabCase())
println(title.toCamelCase())
println(title.toSnakeCase())
let csv = "red,green,blue"
let colors = csv.split(",")
println(colors)
let text = "hello"
println(text.charAt(1))
println(text.codePointAt(1))

Strings also support indexed access through the internal __get__ prototype.

let text = "hello"
println(text[1])

This behaves like charAt.