Nanyx

string

String manipulation functions

The string module provides functions for working with UTF-8 strings.

These functions are attached definitions on String, so when the receiver is known to be a string they can be used without qualification: s \split(on = ","), s.length, s.startsWith("nan").

length

String.length: string -> int

Returns the number of grapheme clusters in the string.

String.length("hello")
-- 5

uppercase

String.uppercase: string -> string
String.uppercase("hello")
-- "HELLO"

lowercase

String.lowercase: string -> string
String.lowercase("HELLO")
-- "hello"

split

String.split: (string, on: string) -> list(string)
String.split("a,b,c", on = ",")
-- ["a", "b", "c"]

contains

String.contains: (string, string) -> bool
String.contains("hello world", "world")
-- true

startsWith

String.startsWith: (string, string) -> bool
String.startsWith("nanyx", "nan")
-- true

endsWith

String.endsWith: (string, string) -> bool
String.endsWith("nanyx", "nyx")
-- true

indexOf

String.indexOf: (string, string) -> #some(int) | #notFound
String.indexOf("hello", "ll")
-- #some(2)

replace

String.replace: (string, pattern: string, replacement: string) -> string
String.replace("hello world", pattern = "world", replacement = "Nanyx")
-- "hello Nanyx"

substring

String.substring: (string, start: int, length: int) -> string
String.substring("nanyx", start = 1, length = 3)
-- "any"

trim

String.trim: string -> string

Removes leading and trailing whitespace.

String.trim("  hello  ")
-- "hello"

concat

String.concat: list(string) -> string
String.concat(["Hello", ", ", "World"])
-- "Hello, World"

join

String.join: (list(string), sep: string) -> string
String.join(["a", "b", "c"], sep = "-")
-- "a-b-c"