Returns a string containing the first character of self
:
s = 'foo' # => "foo" s.chr # => "f"
Returns the byte at zero-based index
as an integer:
s = 'abcde' # => "abcde" s.getbyte(0) # => 97 s.getbyte(1) # => 98
Related: String#setbyte
.
Sets the byte at zero-based index
to integer
; returns integer
:
s = 'abcde' # => "abcde" s.setbyte(0, 98) # => 98 s # => "bbcde"
Related: String#getbyte
.
Returns a substring of self
, or nil
if the substring cannot be constructed.
With integer arguments index
and length
given, returns the substring beginning at the given index
of the given length
(if possible), or nil
if length
is negative or index
falls outside of self
:
s = '0123456789' # => "0123456789" s.byteslice(2) # => "2" s.byteslice(200) # => nil s.byteslice(4, 3) # => "456" s.byteslice(4, 30) # => "456789" s.byteslice(4, -1) # => nil s.byteslice(40, 2) # => nil
In either case above, counts backwards from the end of self
if index
is negative:
s = '0123456789' # => "0123456789" s.byteslice(-4) # => "6" s.byteslice(-4, 3) # => "678"
With Range
argument range
given, returns byteslice(range.begin, range.size)
:
s = '0123456789' # => "0123456789" s.byteslice(4..6) # => "456" s.byteslice(-6..-4) # => "456" s.byteslice(5..2) # => "" # range.size is zero. s.byteslice(40..42) # => nil
In all cases, a returned string has the same encoding as self
:
s.encoding # => #<Encoding:UTF-8> s.byteslice(4).encoding # => #<Encoding:UTF-8>
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.unpack1('H*')+'>' } #=> "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.unpack1('H*')+'>' } #=> "abc\u3042<e380>"
Returns a frozen, possibly pre-existing copy of the string.
The returned String will be deduplicated as long as it does not have any instance variables set on it.
Returns a printable version of self
, enclosed in double-quotes, with special characters escaped, and with non-printing characters replaced by hexadecimal notation:
"hello \n ''".dump # => "\"hello \\n ''\"" "\f\x00\xff\\\"".dump # => "\"\\f\\x00\\xFF\\\\\\\"\""
Related: String#undump
(inverse of String#dump
).
Returns an unescaped version of self
:
s_orig = "\f\x00\xff\\\"" # => "\f\u0000\xFF\\\"" s_dumped = s_orig.dump # => "\"\\f\\x00\\xFF\\\\\\\"\"" s_undumped = s_dumped.undump # => "\f\u0000\xFF\\\"" s_undumped == s_orig # => true
Related: String#dump
(inverse of String#undump
).
Returns a string containing the upcased characters in self
:
s = 'Hello World!' # => "Hello World!" s.upcase # => "HELLO WORLD!"
The casing may be affected by the given options
; see Case Mapping.
Related: String#upcase!
, String#downcase
, String#downcase!
.
Returns a string containing the downcased characters in self
:
s = 'Hello World!' # => "Hello World!" s.downcase # => "hello world!"
The casing may be affected by the given options
; see Case Mapping.
Related: String#downcase!
, String#upcase
, String#upcase!
.
Returns a string containing the characters in self
; the first character is upcased; the remaining characters are downcased:
s = 'hello World!' # => "hello World!" s.capitalize # => "Hello world!"
The casing may be affected by the given options
; see Case Mapping.
Related: String#capitalize!
.
Returns a string containing the characters in self
, with cases reversed; each uppercase character is downcased; each lowercase character is upcased:
s = 'Hello World!' # => "Hello World!" s.swapcase # => "hELLO wORLD!"
The casing may be affected by the given options
; see Case Mapping.
Related: String#swapcase!
.
Upcases the characters in self
; returns self
if any changes were made, nil
otherwise:
s = 'Hello World!' # => "Hello World!" s.upcase! # => "HELLO WORLD!" s # => "HELLO WORLD!" s.upcase! # => nil
The casing may be affected by the given options
; see Case Mapping.
Related: String#upcase
, String#downcase
, String#downcase!
.
Downcases the characters in self
; returns self
if any changes were made, nil
otherwise:
s = 'Hello World!' # => "Hello World!" s.downcase! # => "hello world!" s # => "hello world!" s.downcase! # => nil
The casing may be affected by the given options
; see Case Mapping.
Related: String#downcase
, String#upcase
, String#upcase!
.
Upcases the first character in self
; downcases the remaining characters; returns self
if any changes were made, nil
otherwise:
s = 'hello World!' # => "hello World!" s.capitalize! # => "Hello world!" s # => "Hello world!" s.capitalize! # => nil
The casing may be affected by the given options
; see Case Mapping.
Related: String#capitalize
.
Upcases each lowercase character in self
; downcases uppercase character; returns self
if any changes were made, nil
otherwise:
s = 'Hello World!' # => "Hello World!" s.swapcase! # => "hELLO wORLD!" s # => "Hello World!" ''.swapcase! # => nil
The casing may be affected by the given options
; see Case Mapping.
Related: String#swapcase
.
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
Treats leading characters of str as a string of octal digits (with an optional sign) and returns the corresponding number. Returns 0 if the conversion fails.
"123".oct #=> 83 "-377".oct #=> -255 "bad".oct #=> 0 "0377bad".oct #=> 255
If str
starts with 0
, radix indicators are honored. See Kernel#Integer
.
Divides str into substrings based on a delimiter, returning an array of these substrings.
If pattern is a String
, then its contents are used as the delimiter when splitting str. If pattern is a single space, str is split on whitespace, with leading and trailing whitespace and runs of contiguous whitespace characters ignored.
If pattern is a Regexp
, str is divided where the pattern matches. Whenever the pattern matches a zero-length string, str is split into individual characters. If pattern contains groups, the respective matches will be returned in the array as well.
If pattern is nil
, the value of $;
is used. If $;
is nil
(which is the default), str is split on whitespace as if ‘ ’ were specified.
If the limit parameter is omitted, trailing null fields are suppressed. If limit is a positive number, at most that number of split substrings will be returned (captured groups will be returned as well, but are not counted towards the limit). If limit is 1
, the entire string is returned as the only entry in an array. If negative, there is no limit to the number of fields returned, and trailing null fields are not suppressed.
When the input str
is empty an empty Array
is returned as the string is considered to have no fields to split.
" now's the time ".split #=> ["now's", "the", "time"] " now's the time ".split(' ') #=> ["now's", "the", "time"] " now's the time".split(/ /) #=> ["", "now's", "", "the", "time"] "1, 2.34,56, 7".split(%r{,\s*}) #=> ["1", "2.34", "56", "7"] "hello".split(//) #=> ["h", "e", "l", "l", "o"] "hello".split(//, 3) #=> ["h", "e", "llo"] "hi mom".split(%r{\s*}) #=> ["h", "i", "m", "o", "m"] "mellow yellow".split("ello") #=> ["m", "w y", "w"] "1,2,,3,4,,".split(',') #=> ["1", "2", "", "3", "4"] "1,2,,3,4,,".split(',', 4) #=> ["1", "2", "", "3,4,,"] "1,2,,3,4,,".split(',', -4) #=> ["1", "2", "", "3", "4", "", ""] "1:2:3".split(/(:)()()/, 2) #=> ["1", ":", "", "", "2:3"] "".split(',', -1) #=> []
If a block is given, invoke the block with each split substring.
Returns an array of bytes in str. This is a shorthand for str.each_byte.to_a
.
If a block is given, which is a deprecated form, works the same as each_byte
.
Returns an array of characters in str. This is a shorthand for str.each_char.to_a
.
If a block is given, which is a deprecated form, works the same as each_char
.
Returns a new string with the characters from self
in reverse order.
'stressed'.reverse # => "desserts"