A string is a sequence of characters. It is represented internally by an array of UTF-8 bytes.

Methods

clone()

Get a copy of the string.

IO.println("the cake is a lie".clone()) #> the cake is a lie

cmp(other)

Compare the string to another string. Return an Int indicating if the string is less than other with a negative value, or if the string is greater than other with a positive value, or if the string is equal to other with 0.

IO.println("the cake".cmp("a lie") > 0) #> true
IO.println("the cake".cmp("the cake") == 0) #> true

contains(other)

Return true if other is a part of the string. other must be a character or a string.

IO.println("the cake is a lie".contains("cake")) #> true
IO.println("the cake is a lie".contains('x')) #> false

ends_with(other)

Return true is the string ends with other. other must be a string.

IO.println("the cake is a lie".ends_with("cake")) #> false
IO.println("東京".ends_with("京")) #> true

See also: starts_with(other)

find(needle)

Find needle in the string and return an iterator to the beginning of the first occurrence of needle, or -1 if needle was not found.

IO.println("the cake is a lie".find("cake")) #> 4

find(needle, start)

Find needle in the string, starting at iterator start, and return an iterator to the beginning of the first occurrence of needle, or -1 if needle was not found.

IO.println("the cake is a lie".find("cake", 10)) #> -1

See also: find(needle)

hash

Compute a hash for the string.

IO.println("the cake is a lie".hash) #> -5017247130378745027

iterate(iterator), iterator_value(iterator)

Implement the iterator protocol to iterate over the string.

ltrim(), ltrim(chars)

Remove some characters from the beginning of the string, and return a new string. chars contain the characters that should be removed. By default, it consists in whitespace characters: " \n\t\r".

IO.println(" \n\rthe cake is a lie".ltrim()) #> the cake is a lie
IO.println("the cake is a lie".ltrim(" eht")) #> cake is a lie

replace(old,new)

Replace all the occurrences of old in the string with new and return a new string.

IO.println("the cake is a lie".replace("cake", "pie").replace("lie", "fake"))
#> the pie is a fake

rtrim(), rtrim(chars)

Remove some characters from the end of the string, and return a new string. chars contain the characters that should be removed. By default, it consists in whitespace characters: " \n\t\r".

IO.println("the cake is a lie \n\r".rtrim()) #> the cake is a lie
IO.println("the cake is a lie".rtrim(" eht")) #> the cake is a li

split(sep)

Split the string thanks to the separator sep and return an Array of strings. sep must be a character or a string.

IO.println("the cake is a lie".split(' ')) #> [the, cake, is, a, lie]

starts_with(other)

Return true is the string starts with other. other must be a string.

IO.println("the cake is a lie".starts_with("cake")) #> false
IO.println("東京".starts_with("東")) #> true

See also: ends_with(other)

to_f

Convert the string to a Float.

IO.println("42.0".to_f) #> 42

to_i

Convert the string to an Int.

IO.println("42".to_i) #> 42

to_s

Return the string.

IO.println("the cake is a lie".to_s) #> the cake is a lie

trim(), trim(chars)

Remove some characters from the beginning and the end of the string, and return a new string. chars contain the characters that should be removed. By default, it consists in whitespace characters: " \n\t\r".

IO.println(" \n\rthe cake is a lie \n\r".trim()) #> the cake is a lie
IO.println("the cake is a lie".trim(" eht")) #> cake is a li

Operators

+(other)

Concatenate the string with other and return the result of the concatenation.

IO.println("the cake is " + "a lie") #> the cake is a lie

*(count)

Concatenate the string count times and return the result of the concatenation.

IO.println(" the cake is a lie" * 3)
#>  the cake is a lie the cake is a lie the cake is a lie