A string is a sequence of characters. It is represented internally by an array of UTF-8 bytes.
Methods
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
See also: find(needle, start)
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)
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
See also: rtrim(), rtrim(chars), trim(), trim(chars)
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
See also: ltrim(), ltrim(chars), trim(), trim(chars)
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)
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
See also: ltrim(), ltrim(chars), rtrim(), rtrim(chars)