string

bytes

|String| -> Iterator

Returns an iterator that yields a series of integers representing the bytes contained in the string data.

Example

'Hëy!'.bytes().to_tuple()
# -> (72, 195, 171, 121, 33)

See Also

chars

|String| -> Iterator

Returns an iterator that yields the string's characters as strings.

A 'character' in Koto is defined as being a unicode grapheme cluster.

Note

Note that this is the default iteration behaviour for a string, so calling 'hello'.chars() is equivalent to calling iterator.iter('hello').

Example

'Héllø! 👋'.chars().to_tuple()
# -> ('H', 'é', 'l', 'l', 'ø', '!', ' ', '👋')

See Also

char_indices

|String| -> Iterator

Returns an iterator that yields the indices of each grapheme cluster in the string.

Each cluster is represented as a range, which can then be used to extract the cluster from the string via indexing.

Example

s = 'Hi 👋'

indices = s.char_indices().to_tuple()
# -> (0..1, 1..2, 2..3, 3..7)

s[indices[3]]
# -> 👋

See Also

contains

|String, String| -> Bool

Returns true if the second provided string is a sub-string of the first.

Example

'xyz'.contains 'abc'
# -> false

'xyz'.contains 'yz'
# -> true

'xyz'.contains 'xyz'
# -> true

'xyz'.contains ''
# -> true

ends_with

|String, String| -> Bool

Returns true if the first string ends with the second string.

Example

'abcdef'.ends_with 'def'
# -> true

'xyz'.ends_with 'abc'
# -> false

escape

|String| -> String

Returns the string with characters replaced with escape codes.

For example, newlines get replaced with \n, tabs get replaced with \t.

Example

'👋'.escape()
# -> \u{1f44b}

is_empty

|String| -> Bool

Returns true if the string contains no characters.

Example

'abcdef'.is_empty()
# -> false

''.is_empty()
# -> true

from_bytes

|Iterable| -> String

Returns a string containing the bytes that are produced by the input iterable. The iterable output must contain only Numbers in the 0..=255 range. The resulting sequence of bytes must contain UTF-8 data.

Example

string.from_bytes (72, 195, 171, 121, 33)
# -> Hëy!

See Also

lines

|String| -> Iterator

Returns an iterator that yields the lines contained in the input string.

Note

Lines end with either \r\n or \n.

Example

'foo\nbar\nbaz'.lines().to_tuple()
# -> ('foo', 'bar', 'baz')

'\n\n\n'.lines().to_tuple()
# -> ('', '', '')

repeat

|String, n: Number| -> String

Creates a new string by repeating the input n times.

Example

'abc'.repeat 3
# -> abcabcabc

replace

|String, match: String, replacement: String| -> String

Returns a copy of the input string with all occurrences of the match string replaced with a replacement string.

Example

'10101'.replace '0', 'x'
# -> 1x1x1

split

|String, match: String| -> Iterator

Returns an iterator that yields strings resulting from splitting the first string wherever the match string is encountered.

|String, match: |String| -> Bool| -> Iterator

Returns an iterator that yields strings resulting from splitting the input string based on the result of calling a match function.

The match function will be called for each grapheme in the input string, and splits will occur when the function returns true.

Example

'a,b,c'.split(',').to_tuple()
# -> ('a', 'b', 'c')

'O_O'.split('O').to_tuple()
# -> ('', '_', '')

'x!y?z'.split(|c| c == '!' or c == '?').to_tuple()
# -> ('x', 'y', 'z')

starts_with

|String, match: String| -> Bool

Returns true if the first string starts with the match string.

Example

'abcdef'.starts_with 'abc'
# -> true

'xyz'.starts_with 'abc'
# -> false

to_lowercase

|String| -> String

Returns a lowercase version of the input string.

Example

'HÉLLÖ'.to_lowercase()
# -> héllö

'O_o'.to_lowercase()
# -> o_o

to_number

|String| -> Number

Returns the string converted into a number.

  • 0x, 0o, and 0b prefixes will cause the parsing to treat the input as containing a hexadecimal, octal, or binary number respectively.
  • Otherwise the number is assumed to be base 10, and the presence of a decimal point will produce a float instead of an integer.

If a number can't be produced then Null is returned.

|String, base: Number| -> Number

Returns the string converted into a number given the specified base.

The base must be an integer in the range 2..=36, otherwise an error will be thrown.

If the string contains non-numerical digits then null is returned.

Example

'123'.to_number()
# -> 123

'-8.9'.to_number()
# -> -8.9

'0x7f'.to_number()
# -> 127

'0b10101'.to_number()
# -> 21

'2N9C'.to_number(36)
# -> 123456

to_uppercase

|String| -> String

Returns an uppercase version of the input string.

Example

'héllö'.to_uppercase()
# -> HÉLLÖ

'O_o'.to_uppercase()
# -> O_O

trim

|String| -> String

Returns the string with whitespace at the start and end of the string trimmed.

Example

'   x    '.trim()
# -> x

'     !'.trim()
# -> !