core : string
(string) *
string * num
Create a new string with num copies of the original string.
(string) +
string1 + string2
Concatenates the two strings and creates a new string.
(self) «
string « str
Concatenate a second string onto the current string. Modifies and returns the current string.
a = "a"
a << "b"
a == "ab"
(number) <=>
lhs <=> rhs
Compares two strings. Returns 1 if lhs is greater, -1 if rhs is greater, and 0 if the two are equal.
(boolean) ==
string1 == string2
Compare the contents of two strings.
(boolean) alpha?
string.alpha?
Returns true if the string contains only letters.
"abC".alpha? # Returns true
"X1z".alpha? # Returns false
(boolean) alphanum?
string.alphanum?
Returns true if the string contains only letters and numbers.
"br47".alphanum? # Returns true
"bl_nk".alphanum? # Returns false
(boolean) blank?
string.blank?
Returns true if the string is empty or only contains whitespace characters.
"".blank? # Returns true
"\n".blank? # Returns true
(string) chomp
string.chomp
Create a new string with any line endings removed.
"a\n\n".chomp # Returns "a"
(string) chomp!
string.chomp!
Remove any line endings from string.
a = "a\r\n"
a.chomp!
a # Returns "a"
(array) dice
string.dice
Splits string into an array with each character as as single element.
"abc".dice == ["a" "b" "c"]
(string) downcase
string.downcase
Create a new string with all letters downcased.
(self) downcase!
string.downcase!
Downcase all letters in the string,
(string) each
string.each block
Interate over each character in the string, passing them into the given block.
a = []
"abc".each { letter |
a << letter
}
a # Returns ["a", "b", "c"]
(boolean) empty?
string.empty?
Returns true if the string is empty (zero length), false otherwise.
"".empty? # Returns true
"a".empty? # Returns false
"\n".empty? # Returns false
(number) find
string.find substring
Returns the index of the given substring inside the original string. If no match is found, returns null.
(string) get
string.get index
string.get start, end
Retrieves a section of the string. If a single index is used, returns at most one character. For indexes out of range, returns an empty string. Negative indexes can be used to start from the end of the string. While this method can be called literally, it is more common to use the square bracket ([]
) form.
"abc"[0] # Returns "a"
"abc"[0, 1] # Returns "ab"
"abc"[-1] # Returns "c"
"abc"[-1, -2] # Returns "bc"
"abc"[-1 ,1] # Returns "bc"
(boolean) include?
string.include? substring
string.include? regex
Returns true if the string includes the given substring or regular expression.
(number) length
string.length
Returns the length of the string.
(object) match
string.match regex
string.match regex, index
Returns: object or false This method can be used to find substrings inside a string matching the given regular expression. An optional start index can be provided. If a match is found, an match object is
(string) new
string.new
Create a new string. There should be no reason to use this directly.
(boolean) numeric?
string.numeric?
Returns true if the string only includes decimal digits and an optional leading minus sign.
"five".numeric? # Returns false
"-127".numeric? # Returns true
(string) reverse
string.reverse
Copy and reverse string.
(self) reverse!
string.reverse!
Reverse string.
(boolean) reverse_each
string.reverse_each block
Iterates over each character in string, starting from the end.
a = []
"abc".each { letter |
a << letter
}
a # Returns ["c", "b", "a"]
(self) set
string.set index, character
Sets the given index in the string to the given character.
(array) split
string.split separator
Splits the string into an array based on the given separator, which should be a string. If no separator is given, “ “ is assumed.
a = "hello, there"
a.split #["hello,", "there"]
a.split ", " #["hello", "there"]
a.split "z" #["hello, there"]
(boolean) string?
string.string?
Returns true…because it is a string.
(string) strip
string.strip
Returns a new string with all whitespace removed from the beginning and end of the string.
" a\n".strip # Returns "a"
(self) strip!
string.strip!
Removes all whitespace from the beginning and end of the string.
a = " a\n"
a.strip!
a # Returns "a"
(string) sub
string.sub regex, replacement
string.sub regex, replacement, limit
Returns a new string with instances of the given pattern replaced by the provided replacement string. Instead of a string, the replacement argument can be a function which will be called with each match. The string returned by the function will be used as the replacement. A limit can be used to limit how many replacements are made.
(string) sub
string.sub regex, replacement
Same as using string.sub with a limit of 1.
(self) sub!
string.sub! regex, replacement
Same as using string.sub! with a limit of 1.
(self) sub!
string.sub! regex, replacement
string.sub! regex, replacement, limit
Same as string.sub, but modifies the original string.
(object) to_byte
string.to_byte
Returns: number or array If string is a single character, returns the decimal value associated with that character. If the string is longer than a single character, returns an array of values.
"a".to_byte # 97
"abc".to_byte # [97, 98, 99]
(number) to_f
string.to_f
Interprets the given string as an number.
(number) to_i
string.to_i
string.to_i base
Interprets the given string as an integer. By default the string is expected to be decimal representation.
(self) to_s
string.to_s
Does nothing, just returns the string itself.
(string) upcase
string.upcase
Return a new string with all letters changed to uppercase.
(self) upcase!
string.upcase!
Convert all letters in string to uppercase.