Equality—Returns whether str
== obj
, similar to Object#==
.
If obj
is not an instance of String
but responds to to_str
, then the two strings are compared using obj.==
.
Otherwise, returns similarly to String#eql?
, comparing length and content.
Equality—Returns whether str
== obj
, similar to Object#==
.
If obj
is not an instance of String
but responds to to_str
, then the two strings are compared using obj.==
.
Otherwise, returns similarly to String#eql?
, comparing length and content.
Two strings are equal if they have the same length and content.
Returns a hash based on the string’s length, content and encoding.
See also Object#hash
.
Case-insensitive version of String#<=>
. Currently, case-insensitivity only works on characters A-Z/a-z, not all of Unicode. This is different from String#casecmp?
.
"aBcDeF".casecmp("abcde") #=> 1 "aBcDeF".casecmp("abcdef") #=> 0 "aBcDeF".casecmp("abcdefg") #=> -1 "abcdef".casecmp("ABCDEF") #=> 0
nil
is returned if the two strings have incompatible encodings, or if other_str
is not a string.
"foo".casecmp(2) #=> nil "\u{e4 f6 fc}".encode("ISO-8859-1").casecmp("\u{c4 d6 dc}") #=> nil
Returns true
if str
and other_str
are equal after Unicode case folding, false
if they are not equal.
"aBcDeF".casecmp?("abcde") #=> false "aBcDeF".casecmp?("abcdef") #=> true "aBcDeF".casecmp?("abcdefg") #=> false "abcdef".casecmp?("ABCDEF") #=> true "\u{e4 f6 fc}".casecmp?("\u{c4 d6 dc}") #=> true
nil
is returned if the two strings have incompatible encodings, or if other_str
is not a string.
"foo".casecmp?(2) #=> nil "\u{e4 f6 fc}".encode("ISO-8859-1").casecmp?("\u{c4 d6 dc}") #=> nil
Concatenation—Returns a new String
containing other_str concatenated to str.
"Hello from " + self.to_s #=> "Hello from main"
Copy — Returns a new String
containing integer
copies of the receiver. integer
must be greater than or equal to 0.
"Ho! " * 3 #=> "Ho! Ho! Ho! " "Ho! " * 0 #=> ""
Format—Uses str as a format specification, and returns the result of applying it to arg. If the format specification contains more than one substitution, then arg must be an Array
or Hash
containing the values to be substituted. See Kernel#sprintf
for details of the format string.
"%05d" % 123 #=> "00123" "%-5s: %016x" % [ "ID", self.object_id ] #=> "ID : 00002b054ec93168" "foo = %{foo}" % { :foo => 'bar' } #=> "foo = bar"
Element Reference — If passed a single index
, returns a substring of one character at that index. If passed a start
index and a length
, returns a substring containing length
characters starting at the start
index. If passed a range
, its beginning and end are interpreted as offsets delimiting the substring to be returned.
In these three cases, if an index is negative, it is counted from the end of the string. For the start
and range
cases the starting index is just before a character and an index matching the string’s size. Additionally, an empty string is returned when the starting index for a character range is at the end of the string.
Returns nil
if the initial index falls outside the string or the length is negative.
If a Regexp
is supplied, the matching portion of the string is returned. If a capture
follows the regular expression, which may be a capture group index or name, follows the regular expression that component of the MatchData
is returned instead.
If a match_str
is given, that string is returned if it occurs in the string.
Returns nil
if the regular expression does not match or the match string cannot be found.
a = "hello there" a[1] #=> "e" a[2, 3] #=> "llo" a[2..3] #=> "ll" a[-3, 2] #=> "er" a[7..-2] #=> "her" a[-4..-2] #=> "her" a[-2..-4] #=> "" a[11, 0] #=> "" a[11] #=> nil a[12, 0] #=> nil a[12..-1] #=> nil a[/[aeiou](.)\1/] #=> "ell" a[/[aeiou](.)\1/, 0] #=> "ell" a[/[aeiou](.)\1/, 1] #=> "l" a[/[aeiou](.)\1/, 2] #=> nil a[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "non_vowel"] #=> "l" a[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "vowel"] #=> "e" a["lo"] #=> "lo" a["bye"] #=> nil
Element Assignment—Replaces some or all of the content of str. The portion of the string affected is determined using the same criteria as String#[]
. If the replacement string is not the same length as the text it is replacing, the string will be adjusted accordingly. If the regular expression or string is used as the index doesn’t match a position in the string, IndexError
is raised. If the regular expression form is used, the optional second Integer
allows you to specify which portion of the match to replace (effectively using the MatchData
indexing rules. The forms that take an Integer
will raise an IndexError
if the value is out of range; the Range
form will raise a RangeError
, and the Regexp
and String
will raise an IndexError
on negative match.
Returns the character length of str.
Returns the length of str
in bytes.
"\x80\u3042".bytesize #=> 4 "hello".bytesize #=> 5
Returns true
if str has a length of zero.
"hello".empty? #=> false " ".empty? #=> false "".empty? #=> true
Match—If obj is a Regexp
, use it as a pattern to match against str,and returns the position the match starts, or nil
if there is no match. Otherwise, invokes obj.=~, passing str as an argument. The default =~
in Object
returns nil
.
Note: str =~ regexp
is not the same as regexp =~ str
. Strings captured from named capture groups are assigned to local variables only in the second case.
"cat o' 9 tails" =~ /\d/ #=> 7 "cat o' 9 tails" =~ 9 #=> nil
Converts pattern to a Regexp
(if it isn’t already one), then invokes its match
method on str. If the second parameter is present, it specifies the position in the string to begin the search.
'hello'.match('(.)\1') #=> #<MatchData "ll" 1:"l"> 'hello'.match('(.)\1')[0] #=> "ll" 'hello'.match(/(.)\1/)[0] #=> "ll" 'hello'.match(/(.)\1/, 3) #=> nil 'hello'.match('xx') #=> nil
If a block is given, invoke the block with MatchData
if match succeed, so that you can write
str.match(pat) {|m| ...}
instead of
if m = str.match(pat) ... end
The return value is a value from block execution in this case.
Converts pattern to a Regexp
(if it isn’t already one), then returns a true
or false
indicates whether the regexp is matched str or not without updating $~
and other related variables. If the second parameter is present, it specifies the position in the string to begin the search.
"Ruby".match?(/R.../) #=> true "Ruby".match?(/R.../, 1) #=> false "Ruby".match?(/P.../) #=> false $& #=> nil
Returns the successor to str. The successor is calculated by incrementing characters starting from the rightmost alphanumeric (or the rightmost character if there are no alphanumerics) in the string. Incrementing a digit always results in another digit, and incrementing a letter results in another letter of the same case. Incrementing nonalphanumerics uses the underlying character set’s collating sequence.
If the increment generates a “carry,” the character to the left of it is incremented. This process repeats until there is no carry, adding an additional character if necessary.
"abcd".succ #=> "abce" "THX1138".succ #=> "THX1139" "<<koala>>".succ #=> "<<koalb>>" "1999zzz".succ #=> "2000aaa" "ZZZ9999".succ #=> "AAAA0000" "***".succ #=> "**+"
Equivalent to String#succ
, but modifies the receiver in place.
Returns the successor to str. The successor is calculated by incrementing characters starting from the rightmost alphanumeric (or the rightmost character if there are no alphanumerics) in the string. Incrementing a digit always results in another digit, and incrementing a letter results in another letter of the same case. Incrementing nonalphanumerics uses the underlying character set’s collating sequence.
If the increment generates a “carry,” the character to the left of it is incremented. This process repeats until there is no carry, adding an additional character if necessary.
"abcd".succ #=> "abce" "THX1138".succ #=> "THX1139" "<<koala>>".succ #=> "<<koalb>>" "1999zzz".succ #=> "2000aaa" "ZZZ9999".succ #=> "AAAA0000" "***".succ #=> "**+"
Equivalent to String#succ
, but modifies the receiver in place.
Iterates through successive values, starting at str and ending at other_str inclusive, passing each value in turn to the block. The String#succ
method is used to generate each value. If optional second argument exclusive is omitted or is false, the last value will be included; otherwise it will be excluded.
If no block is given, an enumerator is returned instead.
"a8".upto("b6") {|s| print s, ' ' } for s in "a8".."b6" print s, ' ' end
produces:
a8 a9 b0 b1 b2 b3 b4 b5 b6 a8 a9 b0 b1 b2 b3 b4 b5 b6
If str and other_str contains only ascii numeric characters, both are recognized as decimal numbers. In addition, the width of string (e.g. leading zeros) is handled appropriately.
"9".upto("11").to_a #=> ["9", "10", "11"] "25".upto("5").to_a #=> [] "07".upto("11").to_a #=> ["07", "08", "09", "10", "11"]
Replaces the contents of str with the corresponding values in other_str.
s = "hello" #=> "hello" s.replace "world" #=> "world"