Str
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
Section titled “Str.is_empty”Str.is_empty <- function(string: character) -> logical#: fn(string: character) -> logicalStr.is_empty <- function(string)Whether the string is "".
Str.is_empty("")#> TRUEStr.concat
Section titled “Str.concat”Str.concat <- function(first: character, second: character) -> character#: fn(first: character, second: character) -> characterStr.concat <- function(first, second)first followed by second.
Str.concat("snake", "_case")#> "snake_case"Str.join_with
Section titled “Str.join_with”Str.join_with <- function(strings: list[character], separator: character) -> character#: fn(strings: list[character], separator: character) -> characterStr.join_with <- function(strings, separator)Joins a list of strings with separator between them.
Str.join_with(list("a", "b", "c"), ", ")#> "a, b, c"Str.split_on
Section titled “Str.split_on”Str.split_on <- function(string: character, separator: character) -> list[character]#: fn(string: character, separator: character) -> list[character]Str.split_on <- function(string, separator)Splits at every occurrence of separator.
Str.split_on("a,b,,c", ",")#> list("a", "b", "", "c")Str.repeat
Section titled “Str.repeat”Str.repeat <- function(string: character, count: integer) -> character#: fn(string: character, count: integer) -> characterStr.repeat <- function(string, count)count copies of the string.
Str.repeat("ab", 3L)#> "ababab"Str.starts_with
Section titled “Str.starts_with”Str.starts_with <- function(string: character, prefix: character) -> logical#: fn(string: character, prefix: character) -> logicalStr.starts_with <- function(string, prefix)Whether the string begins with prefix.
Str.starts_with("report.csv", "report")#> TRUEStr.ends_with
Section titled “Str.ends_with”Str.ends_with <- function(string: character, suffix: character) -> logical#: fn(string: character, suffix: character) -> logicalStr.ends_with <- function(string, suffix)Whether the string ends with suffix.
Str.ends_with("report.csv", ".R")#> FALSEStr.contains
Section titled “Str.contains”Str.contains <- function(string: character, part: character) -> logical#: fn(string: character, part: character) -> logicalStr.contains <- function(string, part)Whether part occurs in the string, matched literally.
Str.contains("a.b", ".")#> TRUEStr.trim
Section titled “Str.trim”Str.trim <- function(string: character) -> character#: fn(string: character) -> characterStr.trim <- function(string)Removes leading and trailing whitespace.
Str.trim(" hi ")#> "hi"Str.trim_start
Section titled “Str.trim_start”Str.trim_start <- function(string: character) -> character#: fn(string: character) -> characterStr.trim_start <- function(string)Removes leading whitespace.
Str.trim_start(" hi ")#> "hi "Str.trim_end
Section titled “Str.trim_end”Str.trim_end <- function(string: character) -> character#: fn(string: character) -> characterStr.trim_end <- function(string)Removes trailing whitespace.
Str.trim_end(" hi ")#> " hi"Str.drop_prefix
Section titled “Str.drop_prefix”Str.drop_prefix <- function(string: character, prefix: character) -> character#: fn(string: character, prefix: character) -> characterStr.drop_prefix <- function(string, prefix)Removes prefix if the string begins with it.
Str.drop_prefix("v1.2", "v")#> "1.2"Str.drop_suffix
Section titled “Str.drop_suffix”Str.drop_suffix <- function(string: character, suffix: character) -> character#: fn(string: character, suffix: character) -> characterStr.drop_suffix <- function(string, suffix)Removes suffix if the string ends with it.
Str.drop_suffix("data.csv", ".csv")#> "data"Str.with_prefix
Section titled “Str.with_prefix”Str.with_prefix <- function(string: character, prefix: character) -> character#: fn(string: character, prefix: character) -> characterStr.with_prefix <- function(string, prefix)Puts prefix in front of the string.
Str.with_prefix("world", "hello ")#> "hello world"Str.replace_each
Section titled “Str.replace_each”Str.replace_each <- function(string: character, pattern: character, replacement: character) -> character#: fn(string: character, pattern: character, replacement: character) -> characterStr.replace_each <- function(string, pattern, replacement)Replaces every occurrence of pattern, matched literally.
Str.replace_each("a.b.c", ".", "-")#> "a-b-c"Str.replace_first
Section titled “Str.replace_first”Str.replace_first <- function(string: character, pattern: character, replacement: character) -> character#: fn(string: character, pattern: character, replacement: character) -> characterStr.replace_first <- function(string, pattern, replacement)Replaces the first occurrence of pattern.
Str.replace_first("a.b.c", ".", "-")#> "a-b.c"Str.replace_last
Section titled “Str.replace_last”Str.replace_last <- function(string: character, pattern: character, replacement: character) -> character#: fn(string: character, pattern: character, replacement: character) -> characterStr.replace_last <- function(string, pattern, replacement)Replaces the last occurrence of pattern.
Str.replace_last("a.b.c", ".", "-")#> "a.b-c"Str.split_first
Section titled “Str.split_first”Str.split_first <- function(string: character, separator: character) -> Result<Record{before: character, after: character}, :NotFound>#: fn(string: character, separator: character) -> Result<Record{before: character, after: character}, :NotFound>Str.split_first <- function(string, separator)Splits at the first separator into before and after, or :Err(:NotFound).
Str.split_first("key=value=more", "=")#> :Ok((after = "value=more", before = "key"))Str.split_last
Section titled “Str.split_last”Str.split_last <- function(string: character, separator: character) -> Result<Record{before: character, after: character}, :NotFound>#: fn(string: character, separator: character) -> Result<Record{before: character, after: character}, :NotFound>Str.split_last <- function(string, separator)Splits at the last separator.
Str.split_last("key=value=more", "=")#> :Ok((after = "more", before = "key=value"))Str.count_utf8_bytes
Section titled “Str.count_utf8_bytes”Str.count_utf8_bytes <- function(string: character) -> integer#: fn(string: character) -> integerStr.count_utf8_bytes <- function(string)The number of bytes in the string’s UTF-8 encoding.
Str.count_utf8_bytes("hé")#> 3LStr.to_utf8
Section titled “Str.to_utf8”Str.to_utf8 <- function(string: character) -> list[integer]#: fn(string: character) -> list[integer]Str.to_utf8 <- function(string)The bytes of the string’s UTF-8 encoding.
Str.to_utf8("hé")#> list(104L, 195L, 169L)Str.from_utf8
Section titled “Str.from_utf8”Str.from_utf8 <- function(bytes: list[integer]) -> Result<character, :BadUtf8>#: fn(bytes: list[integer]) -> Result<character, :BadUtf8>Str.from_utf8 <- function(bytes)The string a list of bytes encodes, or :Err(:BadUtf8).
Str.from_utf8(list(104L, 105L))#> :Ok("hi")Str.from_utf8_lossy
Section titled “Str.from_utf8_lossy”Str.from_utf8_lossy <- function(bytes: list[integer]) -> character#: fn(bytes: list[integer]) -> characterStr.from_utf8_lossy <- function(bytes)Decodes UTF-8 bytes, replacing each invalid sequence with U+FFFD.
Str.from_utf8_lossy(list(104L, 255L, 105L))#> "h�i"Str.to_integer
Section titled “Str.to_integer”Str.to_integer <- function(string: character) -> Result<integer, :InvalidNumStr>#: fn(string: character) -> Result<integer, :InvalidNumStr>Str.to_integer <- function(string)Parses a whole number that fits an integer, or :Err(:InvalidNumStr).
Str.to_integer("4.2")#> :Err(:InvalidNumStr)Str.to_double
Section titled “Str.to_double”Str.to_double <- function(string: character) -> Result<double, :InvalidNumStr>#: fn(string: character) -> Result<double, :InvalidNumStr>Str.to_double <- function(string)Parses a decimal number, or :Err(:InvalidNumStr); NA and NaN are not numbers.
Str.to_double("4.5e1")#> :Ok(45)Str.with_ascii_lowercased
Section titled “Str.with_ascii_lowercased”Str.with_ascii_lowercased <- function(string: character) -> character#: fn(string: character) -> characterStr.with_ascii_lowercased <- function(string)Lowercases the ASCII letters and leaves every other character alone.
Str.with_ascii_lowercased("Straße ABC")#> "straße abc"Str.with_ascii_uppercased
Section titled “Str.with_ascii_uppercased”Str.with_ascii_uppercased <- function(string: character) -> character#: fn(string: character) -> characterStr.with_ascii_uppercased <- function(string)Uppercases the ASCII letters.
Str.with_ascii_uppercased("straße abc")#> "STRAßE ABC"Str.caseless_ascii_equals
Section titled “Str.caseless_ascii_equals”Str.caseless_ascii_equals <- function(first: character, second: character) -> logical#: fn(first: character, second: character) -> logicalStr.caseless_ascii_equals <- function(first, second)Whether two strings are equal when ASCII case is ignored.
Str.caseless_ascii_equals("Content-Type", "content-type")#> TRUEStr.walk_utf8
Section titled “Str.walk_utf8”Str.walk_utf8 <- function<S>(string: character, state: S, f: fn(S, integer) -> S) -> S#: <S> fn(string: character, state: S, f: fn(S, integer) -> S) -> SStr.walk_utf8 <- function(string, state, f)Folds the string’s UTF-8 bytes: f(state, byte) gives the next state.
Str.walk_utf8("abc", 0L, function(total, byte) total + byte)#> 294LStr.walk_utf8_with_index
Section titled “Str.walk_utf8_with_index”Str.walk_utf8_with_index <- function<S>(string: character, state: S, f: fn(S, integer, integer) -> S) -> S#: <S> fn(string: character, state: S, f: fn(S, integer, integer) -> S) -> SStr.walk_utf8_with_index <- function(string, state, f)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)Str.to_utf16
Section titled “Str.to_utf16”Str.to_utf16 <- function(string: character) -> list[integer]#: fn(string: character) -> list[integer]Str.to_utf16 <- function(string)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)Str.from_utf16
Section titled “Str.from_utf16”Str.from_utf16 <- function(units: list[integer]) -> Result<character, :BadUtf16>#: fn(units: list[integer]) -> Result<character, :BadUtf16>Str.from_utf16 <- function(units)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)Str.from_utf16_lossy
Section titled “Str.from_utf16_lossy”Str.from_utf16_lossy <- function(units: list[integer]) -> character#: fn(units: list[integer]) -> characterStr.from_utf16_lossy <- function(units)Decodes UTF-16 code units, replacing each unpaired surrogate and each zero unit with U+FFFD.
Str.from_utf16_lossy(list(104L, 105L))#> "hi"Str.to_utf32
Section titled “Str.to_utf32”Str.to_utf32 <- function(string: character) -> list[integer]#: fn(string: character) -> list[integer]Str.to_utf32 <- function(string)The string’s Unicode code points, one per character.
Str.to_utf32("h\u00e9")#> list(104L, 233L)Str.from_utf32
Section titled “Str.from_utf32”Str.from_utf32 <- function(code_points: list[integer]) -> Result<character, :BadUtf32>#: fn(code_points: list[integer]) -> Result<character, :BadUtf32>Str.from_utf32 <- function(code_points)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)Str.from_utf32_lossy
Section titled “Str.from_utf32_lossy”Str.from_utf32_lossy <- function(code_points: list[integer]) -> character#: fn(code_points: list[integer]) -> characterStr.from_utf32_lossy <- function(code_points)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"