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"
returns the indexth byte as an integer.
modifies the indexth byte as integer.
Byte Reference—If passed a single Integer
, returns a substring of one byte at that position. If passed two Integer
objects, returns a substring starting at the offset given by the first, and a length given by the second. If given a Range
, a substring containing bytes at offsets given by the range is returned. In all three cases, if an offset is negative, it is counted from the end of str. Returns nil
if the initial offset falls outside the string, the length is negative, or the beginning of the range is greater than the end. The encoding of the resulted string keeps original encoding.
"hello".byteslice(1) #=> "e" "hello".byteslice(-1) #=> "o" "hello".byteslice(1, 2) #=> "el" "\x80\u3042".byteslice(1, 3) #=> "\u3042" "\x03\u3042\xff".byteslice(1..3) #=> "\u3042"
If the string is invalid byte sequence then replace invalid bytes with given replacement character, else returns self. If block is given, replace invalid bytes with returned value of the block.
"abc\u3042\x81".scrub #=> "abc\u3042\uFFFD" "abc\u3042\x81".scrub("*") #=> "abc\u3042*" "abc\u3042\xE3\x80".scrub{|bytes| '<'+bytes.unpack('H*')[0]+'>' } #=> "abc\u3042<e380>"
If the string is invalid byte sequence then replace invalid bytes with given replacement character, else returns self. If block is given, replace invalid bytes with returned value of the block.
"abc\u3042\x81".scrub! #=> "abc\u3042\uFFFD" "abc\u3042\x81".scrub!("*") #=> "abc\u3042*" "abc\u3042\xE3\x80".scrub!{|bytes| '<'+bytes.unpack('H*')[0]+'>' } #=> "abc\u3042<e380>"
If the string is frozen, then return duplicated mutable string.
If the string is not frozen, then return the string itself.
Returns a frozen, possibly pre-existing copy of the string.
The string will be deduplicated as long as it does not have any instance variables set on it.
Returns a quoted version of the string with all non-printing characters replaced by \xHH
notation and all special characters escaped.
This method can be used for round-trip: if the resulting new_str
is eval’ed, it will produce the original string.
"hello \n ''".dump #=> "\"hello \\n ''\"" "\f\x00\xff\\\"".dump #=> "\"\\f\\x00\\xFF\\\\\\\"\""
See also String#undump
.
Returns an unescaped version of the string. This does the inverse of String#dump
.
"\"hello \\n ''\"".undump #=> "hello \n ''"
Returns a copy of str with all lowercase letters replaced with their uppercase counterparts.
See String#downcase
for meaning of options
and use with different encodings.
"hEllO".upcase #=> "HELLO"
Returns a copy of str with all uppercase letters replaced with their lowercase counterparts. Which letters exactly are replaced, and by which other letters, depends on the presence or absence of options, and on the encoding
of the string.
The meaning of the options
is as follows:
Full Unicode case mapping, suitable for most languages (see :turkic and :lithuanian options below for exceptions). Context-dependent case mapping as described in Table 3-14 of the Unicode standard is currently not supported.
Only the ASCII region, i.e. the characters “A” to “Z” and “a” to “z”, are affected. This option cannot be combined with any other option.
Full Unicode case mapping, adapted for Turkic languages (Turkish, Azerbaijani, …). This means that upper case I is mapped to lower case dotless i, and so on.
Currently, just full Unicode case mapping. In the future, full Unicode case mapping adapted for Lithuanian (keeping the dot on the lower case i even if there is an accent on top).
Only available on downcase
and downcase!
. Unicode case folding, which is more far-reaching than Unicode case mapping. This option currently cannot be combined with any other option (i.e. there is currently no variant for turkic languages).
Please note that several assumptions that are valid for ASCII-only case conversions do not hold for more general case conversions. For example, the length of the result may not be the same as the length of the input (neither in characters nor in bytes), some roundtrip assumptions (e.g. str.downcase == str.upcase.downcase) may not apply, and Unicode normalization (i.e. String#unicode_normalize
) is not necessarily maintained by case mapping operations.
Non-ASCII case mapping/folding is currently supported for UTF-8, UTF-16BE/LE, UTF-32BE/LE, and ISO-8859-1~16 Strings/Symbols. This support will be extended to other encodings.
"hEllO".downcase #=> "hello"
Returns a copy of str with the first character converted to uppercase and the remainder to lowercase.
See String#downcase
for meaning of options
and use with different encodings.
"hello".capitalize #=> "Hello" "HELLO".capitalize #=> "Hello" "123ABC".capitalize #=> "123abc"
Returns a copy of str with uppercase alphabetic characters converted to lowercase and lowercase characters converted to uppercase.
See String#downcase
for meaning of options
and use with different encodings.
"Hello".swapcase #=> "hELLO" "cYbEr_PuNk11".swapcase #=> "CyBeR_pUnK11"
Upcases the contents of str, returning nil
if no changes were made.
See String#downcase
for meaning of options
and use with different encodings.
Downcases the contents of str, returning nil
if no changes were made.
See String#downcase
for meaning of options
and use with different encodings.
Modifies str by converting the first character to uppercase and the remainder to lowercase. Returns nil
if no changes are made. There is an exception for modern Georgian (mkhedruli/MTAVRULI), where the result is the same as for String#downcase
, to avoid mixed case.
See String#downcase
for meaning of options
and use with different encodings.
a = "hello" a.capitalize! #=> "Hello" a #=> "Hello" a.capitalize! #=> nil
Equivalent to String#swapcase
, but modifies the receiver in place, returning str, or nil
if no changes were made.
See String#downcase
for meaning of options
and use with different encodings.
Treats leading characters from str as a string of hexadecimal digits (with an optional sign and an optional 0x
) and returns the corresponding number. Zero is returned on error.
"0x0a".hex #=> 10 "-1234".hex #=> -4660 "0".hex #=> 0 "wombat".hex #=> 0