Skip to content
ry

Functions over single strings. Patterns and separators are matched literally, never as regular expressions.

Function Summary
Str.from_r Converts an R string into a Str, refusing NA and a length other than one.
Str.is_empty Whether the string is "".
Str.concat first followed by second.
Str.join_with Joins a list of strings with separator between them.
Str.split_on Splits at every occurrence of separator.
Str.repeat count copies of the string.
Str.starts_with Whether the string begins with prefix.
Str.ends_with Whether the string ends with suffix.
Str.contains Whether part occurs in the string, matched literally.
Str.trim Removes leading and trailing whitespace.
Str.trim_start Removes leading whitespace.
Str.trim_end Removes trailing whitespace.
Str.drop_prefix Removes prefix if the string begins with it.
Str.drop_suffix Removes suffix if the string ends with it.
Str.with_prefix Puts prefix in front of the string.
Str.replace_each Replaces every occurrence of pattern, matched literally.
Str.replace_first Replaces the first occurrence of pattern.
Str.replace_last Replaces the last occurrence of pattern.
Str.split_first Splits at the first separator into before and after, or :Err(:NotFound).
Str.split_last Splits at the last separator.
Str.count_utf8_bytes The number of bytes in the string’s UTF-8 encoding.
Str.to_utf8 The bytes of the string’s UTF-8 encoding.
Str.from_utf8 The string a list of bytes encodes, or :Err(:BadUtf8).
Str.from_utf8_lossy Decodes UTF-8 bytes, replacing each invalid sequence with U+FFFD.
Str.to_i64 Parses a whole number that fits an I64, or :Err(:InvalidNumStr).
Str.to_f64 Parses a decimal number, or :Err(:InvalidNumStr); NA and NaN are not numbers.
Str.with_ascii_lowercased Lowercases the ASCII letters and leaves every other character alone.
Str.with_ascii_uppercased Uppercases the ASCII letters.
Str.caseless_ascii_equals Whether two strings are equal when ASCII case is ignored.
Str.walk_utf8 Folds the string’s UTF-8 bytes: f(state, byte) gives the next state.
Str.walk_utf8_with_index Folds the string’s UTF-8 bytes, passing f each byte’s 1-based position too.
Str.to_utf16 The string’s UTF-16 code units, each an integer from 0 to 65535.
Str.from_utf16 Decodes UTF-16 code units, or :Err(:BadUtf16) for an unpaired surrogate or a zero unit, which an R string cannot hold.
Str.from_utf16_lossy Decodes UTF-16 code units, replacing each unpaired surrogate and each zero unit with U+FFFD.
Str.to_utf32 The string’s Unicode code points, one per character.
Str.from_utf32 Builds a string from Unicode code points, or :Err(:BadUtf32) for a number that is not one (a surrogate, a negative number, or one above 1114111) and for zero.
Str.from_utf32_lossy Builds a string from Unicode code points, replacing each number that is not one with U+FFFD.
Str.from_r <- function(value: character) -> Str

Converts an R string into a Str, refusing NA and a length other than one.

Source

Str.is_empty <- function(string: Str) -> Bool

Whether the string is "".

Str.is_empty("")
#> TRUE

Source

Str.concat <- function(first: Str, second: Str) -> Str

first followed by second.

Str.concat("snake", "_case")
#> "snake_case"

Source

Str.join_with <- function(strings: List<Str>, separator: Str) -> Str

Joins a list of strings with separator between them.

Str.join_with(["a", "b", "c"], ", ")
#> "a, b, c"

Source

Str.split_on <- function(string: Str, separator: Str) -> List<Str>

Splits at every occurrence of separator.

Str.split_on("a,b,,c", ",")
#> ["a", "b", "", "c"]

Source

Str.repeat <- function(string: Str, count: I64) -> Str

count copies of the string.

Str.repeat("ab", 3)
#> "ababab"

Source

Str.starts_with <- function(string: Str, prefix: Str) -> Bool

Whether the string begins with prefix.

Str.starts_with("report.csv", "report")
#> TRUE

Source

Str.ends_with <- function(string: Str, suffix: Str) -> Bool

Whether the string ends with suffix.

Str.ends_with("report.csv", ".R")
#> FALSE

Source

Str.contains <- function(string: Str, part: Str) -> Bool

Whether part occurs in the string, matched literally.

Str.contains("a.b", ".")
#> TRUE

Source

Str.trim <- function(string: Str) -> Str

Removes leading and trailing whitespace.

Str.trim(" hi ")
#> "hi"

Source

Str.trim_start <- function(string: Str) -> Str

Removes leading whitespace.

Str.trim_start(" hi ")
#> "hi "

Source

Str.trim_end <- function(string: Str) -> Str

Removes trailing whitespace.

Str.trim_end(" hi ")
#> " hi"

Source

Str.drop_prefix <- function(string: Str, prefix: Str) -> Str

Removes prefix if the string begins with it.

Str.drop_prefix("v1.2", "v")
#> "1.2"

Source

Str.drop_suffix <- function(string: Str, suffix: Str) -> Str

Removes suffix if the string ends with it.

Str.drop_suffix("data.csv", ".csv")
#> "data"

Source

Str.with_prefix <- function(string: Str, prefix: Str) -> Str

Puts prefix in front of the string.

Str.with_prefix("world", "hello ")
#> "hello world"

Source

Str.replace_each <- function(string: Str, pattern: Str, replacement: Str) -> Str

Replaces every occurrence of pattern, matched literally.

Str.replace_each("a.b.c", ".", "-")
#> "a-b-c"

Source

Str.replace_first <- function(string: Str, pattern: Str, replacement: Str) -> Str

Replaces the first occurrence of pattern.

Str.replace_first("a.b.c", ".", "-")
#> "a-b.c"

Source

Str.replace_last <- function(string: Str, pattern: Str, replacement: Str) -> Str

Replaces the last occurrence of pattern.

Str.replace_last("a.b.c", ".", "-")
#> "a.b-c"

Source

Str.split_first <- function(string: Str, separator: Str) -> Result<Record{before: Str, after: Str}, :NotFound>

Splits at the first separator into before and after, or :Err(:NotFound).

Str.split_first("key=value=more", "=")
#> :Ok((after = "value=more", before = "key"))

Source

Str.split_last <- function(string: Str, separator: Str) -> Result<Record{before: Str, after: Str}, :NotFound>

Splits at the last separator.

Str.split_last("key=value=more", "=")
#> :Ok((after = "more", before = "key=value"))

Source

Str.count_utf8_bytes <- function(string: Str) -> I64

The number of bytes in the string’s UTF-8 encoding.

Str.count_utf8_bytes("hé")
#> 3i64

Source

Str.to_utf8 <- function(string: Str) -> List<I64>

The bytes of the string’s UTF-8 encoding.

Str.to_utf8("hé")
#> [104i64, 195i64, 169i64]

Source

Str.from_utf8 <- function(bytes: List<I64>) -> Result<Str, :BadUtf8>

The string a list of bytes encodes, or :Err(:BadUtf8).

Str.from_utf8([104, 105])
#> :Ok("hi")

Source

Str.from_utf8_lossy <- function(bytes: List<I64>) -> Str

Decodes UTF-8 bytes, replacing each invalid sequence with U+FFFD.

Str.from_utf8_lossy([104, 255, 105])
#> "h�i"

Source

Str.to_i64 <- function(string: Str) -> Result<I64, :InvalidNumStr>

Parses a whole number that fits an I64, or :Err(:InvalidNumStr).

Str.to_i64("4.2")
#> :Err(:InvalidNumStr)

Source

Str.to_f64 <- function(string: Str) -> Result<F64, :InvalidNumStr>

Parses a decimal number, or :Err(:InvalidNumStr); NA and NaN are not numbers.

Str.to_f64("4.5e1")
#> :Ok(45)

Source

Str.with_ascii_lowercased <- function(string: Str) -> Str

Lowercases the ASCII letters and leaves every other character alone.

Str.with_ascii_lowercased("Straße ABC")
#> "straße abc"

Source

Str.with_ascii_uppercased <- function(string: Str) -> Str

Uppercases the ASCII letters.

Str.with_ascii_uppercased("straße abc")
#> "STRAßE ABC"

Source

Str.caseless_ascii_equals <- function(first: Str, second: Str) -> Bool

Whether two strings are equal when ASCII case is ignored.

Str.caseless_ascii_equals("Content-Type", "content-type")
#> TRUE

Source

Str.walk_utf8 <- function<S>(string: Str, state: S, f: fn(S, I64) -> S) -> S

Folds the string’s UTF-8 bytes: f(state, byte) gives the next state.

Str.walk_utf8("abc", 0, function(total, byte) total + Num.to_i64(byte))
#> 294i64

Source

Str.walk_utf8_with_index <- function<S>(string: Str, state: S, f: fn(S, I64, I64) -> S) -> S

Folds the string’s UTF-8 bytes, passing f each byte’s 1-based position too.

Str.walk_utf8_with_index("ab", [], function(seen, byte, i) List.append(seen, Num.to_i64(byte) * i))
#> [97i64, 196i64]

Source

Str.to_utf16 <- function(string: Str) -> List<I64>

The string’s UTF-16 code units, each an integer from 0 to 65535. A character outside the Basic Multilingual Plane takes two units, a surrogate pair.

Str.to_utf16("h\U0001F600")
#> [104i64, 55357i64, 56832i64]

Source

Str.from_utf16 <- function(units: List<I64>) -> Result<Str, :BadUtf16>

Decodes UTF-16 code units, or :Err(:BadUtf16) for an unpaired surrogate or a zero unit, which an R string cannot hold. A number outside 0 to 65535 is an error.

Str.from_utf16([104, 55357])
#> :Err(:BadUtf16)

Source

Str.from_utf16_lossy <- function(units: List<I64>) -> Str

Decodes UTF-16 code units, replacing each unpaired surrogate and each zero unit with U+FFFD.

Str.from_utf16_lossy([104, 105])
#> "hi"

Source

Str.to_utf32 <- function(string: Str) -> List<I64>

The string’s Unicode code points, one per character.

Str.to_utf32("h\u00e9")
#> [104i64, 233i64]

Source

Str.from_utf32 <- function(code_points: List<I64>) -> Result<Str, :BadUtf32>

Builds a string from Unicode code points, or :Err(:BadUtf32) for a number that is not one (a surrogate, a negative number, or one above 1114111) and for zero.

Str.from_utf32([55296])
#> :Err(:BadUtf32)

Source

Str.from_utf32_lossy <- function(code_points: List<I64>) -> Str

Builds a string from Unicode code points, replacing each number that is not one with U+FFFD.

Str.from_utf32_lossy([82, 121])
#> "Ry"

Source