Skip to content
ry

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

Function Summary
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_integer Parses a whole number that fits an integer, or :Err(:InvalidNumStr).
Str.to_double 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.is_empty <- function(string: character) -> logical

Whether the string is "".

Str.is_empty("")
#> TRUE

Source

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

first followed by second.

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

Source

Str.join_with <- function(strings: list[character], separator: character) -> character

Joins a list of strings with separator between them.

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

Source

Str.split_on <- function(string: character, separator: character) -> list[character]

Splits at every occurrence of separator.

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

Source

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

count copies of the string.

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

Source

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

Whether the string begins with prefix.

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

Source

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

Whether the string ends with suffix.

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

Source

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

Whether part occurs in the string, matched literally.

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

Source

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

Removes leading and trailing whitespace.

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

Source

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

Removes leading whitespace.

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

Source

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

Removes trailing whitespace.

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

Source

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

Removes prefix if the string begins with it.

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

Source

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

Removes suffix if the string ends with it.

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

Source

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

Puts prefix in front of the string.

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

Source

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

Replaces every occurrence of pattern, matched literally.

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

Source

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

Replaces the first occurrence of pattern.

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

Source

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

Replaces the last occurrence of pattern.

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

Source

Str.split_first <- function(string: character, separator: character) -> Result<Record{before: character, after: character}, :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: character, separator: character) -> Result<Record{before: character, after: character}, :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: character) -> integer

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

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

Source

Str.to_utf8 <- function(string: character) -> list[integer]

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

Str.to_utf8("hé")
#> list(104L, 195L, 169L)

Source

Str.from_utf8 <- function(bytes: list[integer]) -> Result<character, :BadUtf8>

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

Str.from_utf8(list(104L, 105L))
#> :Ok("hi")

Source

Str.from_utf8_lossy <- function(bytes: list[integer]) -> character

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

Str.from_utf8_lossy(list(104L, 255L, 105L))
#> "h�i"

Source

Str.to_integer <- function(string: character) -> Result<integer, :InvalidNumStr>

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

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

Source

Str.to_double <- function(string: character) -> Result<double, :InvalidNumStr>

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

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

Source

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

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: character) -> character

Uppercases the ASCII letters.

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

Source

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

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: character, state: S, f: fn(S, integer) -> S) -> S

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

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

Source

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

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

Str.walk_utf8_with_index("ab", list(), function(seen, byte, i) List.append(seen, byte * i))
#> list(97L, 196L)

Source

Str.to_utf16 <- function(string: character) -> list[integer]

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")
#> list(104L, 55357L, 56832L)

Source

Str.from_utf16 <- function(units: list[integer]) -> Result<character, :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(list(104L, 55357L))
#> :Err(:BadUtf16)

Source

Str.from_utf16_lossy <- function(units: list[integer]) -> character

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

Str.from_utf16_lossy(list(104L, 105L))
#> "hi"

Source

Str.to_utf32 <- function(string: character) -> list[integer]

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

Str.to_utf32("h\u00e9")
#> list(104L, 233L)

Source

Str.from_utf32 <- function(code_points: list[integer]) -> Result<character, :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(list(55296L))
#> :Err(:BadUtf32)

Source

Str.from_utf32_lossy <- function(code_points: list[integer]) -> character

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

Str.from_utf32_lossy(list(82L, 121L))
#> "Ry"

Source