Skip to content

String-related functions

Defined in: core::strings

str_length

The length of a string.

fn str_length(s: String) -> Scalar

Example

str_length("Numbat")

    = 6
Run this example

str_slice

Subslice of a string.

fn str_slice(start: Scalar, end: Scalar, s: String) -> String

Example

str_slice(3, 6, "Numbat")

    = "bat"    [String]
Run this example

chr

Get a single-character string from a Unicode code point.

fn chr(n: Scalar) -> String

Example

0x2764 -> chr

    = "❤"    [String]
Run this example

ord

Get the Unicode code point of the first character in a string.

fn ord(s: String) -> Scalar

Example

"❤" -> ord

    = 10084
Run this example

lowercase

Convert a string to lowercase.

fn lowercase(s: String) -> String

Example

lowercase("Numbat")

    = "numbat"    [String]
Run this example

uppercase

Convert a string to uppercase.

fn uppercase(s: String) -> String

Example

uppercase("Numbat")

    = "NUMBAT"    [String]
Run this example

str_append

Concatenate two strings.

fn str_append(a: String, b: String) -> String

Example

"!" |> str_append("Numbat")

    = "Numbat!"    [String]
Run this example

Example

str_append("Numbat", "!")

    = "Numbat!"    [String]
Run this example

str_prepend

Concatenate two strings.

fn str_prepend(a: String, b: String) -> String

Example

"Numbat" |> str_prepend("!")

    = "Numbat!"    [String]
Run this example

Example

str_prepend("!", "Numbat")

    = "Numbat!"    [String]
Run this example

str_find

Find the first occurrence of a substring in a string.

fn str_find(needle: String, haystack: String) -> Scalar

Example

str_find("typed", "Numbat is a statically typed programming language.")

    = 23
Run this example

str_contains

Check if a string contains a substring.

fn str_contains(needle: String, haystack: String) -> Bool

Example

str_contains("typed", "Numbat is a statically typed programming language.")

    = true    [Bool]
Run this example

str_replace

Replace all occurrences of a substring in a string.

fn str_replace(pattern: String, replacement: String, s: String) -> String

Example

str_replace("statically typed programming language", "scientific calculator", "Numbat is a statically typed programming language.")

    = "Numbat is a scientific calculator."    [String]
Run this example

str_repeat

Repeat the input string n times.

fn str_repeat(n: Scalar, a: String) -> String

Example

str_repeat(4, "abc")

    = "abcabcabcabc"    [String]
Run this example

base

Convert a number to the given base.

fn base(b: Scalar, x: Scalar) -> String

Example

42 |> base(16)

    = "2a"    [String]
Run this example

bin

Get a binary representation of a number.

fn bin(x: Scalar) -> String

Example

42 -> bin

    = "0b101010"    [String]
Run this example

oct

Get an octal representation of a number.

fn oct(x: Scalar) -> String

Example

42 -> oct

    = "0o52"    [String]
Run this example

dec

Get a decimal representation of a number.

fn dec(x: Scalar) -> String

Example

0b111 -> dec

    = "7"    [String]
Run this example

hex

Get a hexadecimal representation of a number.

fn hex(x: Scalar) -> String

Example

2^31-1 -> hex

    = "0x7fffffff"    [String]
Run this example