Like String#chomp
, but modifies self
in place; returns nil
if no modification made, self
otherwise.
Returns a copy of self
with characters specified by selectors
removed (see Multiple Character Selectors):
"hello".delete "l","lo" #=> "heo" "hello".delete "lo" #=> "he" "hello".delete "aeiou", "^e" #=> "hell" "hello".delete "ej-m" #=> "ho"
Returns a copy of self
with characters specified by selectors
“squeezed” (see Multiple Character Selectors):
“Squeezed” means that each multiple-character run of a selected character is squeezed down to a single character; with no arguments given, squeezes all characters:
"yellow moon".squeeze #=> "yelow mon" " now is the".squeeze(" ") #=> " now is the" "putters shoot balls".squeeze("m-z") #=> "puters shot balls"
Returns the total number of characters in self
that are specified by the given selectors
(see Multiple Character Selectors):
a = "hello world" a.count "lo" #=> 5 a.count "lo", "o" #=> 2 a.count "hello", "^l" #=> 4 a.count "ej-m" #=> 4 "hello^world".count "\\^aeiou" #=> 4 "hello-world".count "a\\-eo" #=> 4 c = "hello world\\r\\n" c.count "\\" #=> 2 c.count "\\A" #=> 0 c.count "X-\\w" #=> 3
Like String#delete
, but modifies self
in place. Returns self
if any changes were made, nil
otherwise.
Like String#squeeze
, but modifies self
in place. Returns self
if any changes were made, nil
otherwise.
Returns a basic n
-bit checksum of the characters in self
; the checksum is the sum of the binary value of each byte in self
, modulo 2**n - 1
:
'hello'.sum # => 532 'hello'.sum(4) # => 4 'hello'.sum(64) # => 532 'тест'.sum # => 1405 'こんにちは'.sum # => 2582
This is not a particularly strong checksum.
Returns the substring of self
specified by the arguments. See examples at String Slices.
Removes and returns the substring of self
specified by the arguments. See String Slices.
A few examples:
string = "This is a string" string.slice!(2) #=> "i" string.slice!(3..6) #=> " is " string.slice!(/s.*t/) #=> "sa st" string.slice!("r") #=> "r" string #=> "Thing"
Returns a 3-element array of substrings of self
.
Matches a pattern against self
, scanning from the beginning. The pattern is:
string_or_regexp
itself, if it is a Regexp
.
Regexp.quote(string_or_regexp)
, if string_or_regexp
is a string.
If the pattern is matched, returns pre-match, first-match, post-match:
'hello'.partition('l') # => ["he", "l", "lo"] 'hello'.partition('ll') # => ["he", "ll", "o"] 'hello'.partition('h') # => ["", "h", "ello"] 'hello'.partition('o') # => ["hell", "o", ""] 'hello'.partition(/l+/) #=> ["he", "ll", "o"] 'hello'.partition('') # => ["", "", "hello"] 'тест'.partition('т') # => ["", "т", "ест"] 'こんにちは'.partition('に') # => ["こん", "に", "ちは"]
If the pattern is not matched, returns a copy of self
and two empty strings:
'hello'.partition('x') # => ["hello", "", ""]
Related: String#rpartition
, String#split
.
Returns a 3-element array of substrings of self
.
Matches a pattern against self
, scanning backwards from the end. The pattern is:
string_or_regexp
itself, if it is a Regexp
.
Regexp.quote(string_or_regexp)
, if string_or_regexp
is a string.
If the pattern is matched, returns pre-match, last-match, post-match:
'hello'.rpartition('l') # => ["hel", "l", "o"] 'hello'.rpartition('ll') # => ["he", "ll", "o"] 'hello'.rpartition('h') # => ["", "h", "ello"] 'hello'.rpartition('o') # => ["hell", "o", ""] 'hello'.rpartition(/l+/) # => ["hel", "l", "o"] 'hello'.rpartition('') # => ["hello", "", ""] 'тест'.rpartition('т') # => ["тес", "т", ""] 'こんにちは'.rpartition('に') # => ["こん", "に", "ちは"]
If the pattern is not matched, returns two empty strings and a copy of self
:
'hello'.rpartition('x') # => ["", "", "hello"]
Related: String#partition
, String#split
.
Returns a copy of self
that has ASCII-8BIT encoding; the underlying bytes are not modified:
s = "\x99" s.encoding # => #<Encoding:UTF-8> t = s.b # => "\x99" t.encoding # => #<Encoding:ASCII-8BIT> s = "\u4095" # => "䂕" s.encoding # => #<Encoding:UTF-8> s.bytes # => [228, 130, 149] t = s.b # => "\xE4\x82\x95" t.encoding # => #<Encoding:ASCII-8BIT> t.bytes # => [228, 130, 149]
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 and is not a String
subclass.
Note that -string
variant is more convenient for defining constants:
FILENAME = -'config/database.yml'
while dedup
is better suitable for using the method in chains of calculations:
@url_list.concat(urls.map(&:dedup))
Returns a copy of self
transcoded as determined by dst_encoding
. By default, raises an exception if self
contains an invalid byte or a character not defined in dst_encoding
; that behavior may be modified by encoding options; see below.
With no arguments:
Uses the same encoding if Encoding.default_internal
is nil
(the default):
Encoding.default_internal # => nil s = "Ruby\x99".force_encoding('Windows-1252') s.encoding # => #<Encoding:Windows-1252> s.bytes # => [82, 117, 98, 121, 153] t = s.encode # => "Ruby\x99" t.encoding # => #<Encoding:Windows-1252> t.bytes # => [82, 117, 98, 121, 226, 132, 162]
Otherwise, uses the encoding Encoding.default_internal
:
Encoding.default_internal = 'UTF-8' t = s.encode # => "Ruby™" t.encoding # => #<Encoding:UTF-8>
With only argument dst_encoding
given, uses that encoding:
s = "Ruby\x99".force_encoding('Windows-1252') s.encoding # => #<Encoding:Windows-1252> t = s.encode('UTF-8') # => "Ruby™" t.encoding # => #<Encoding:UTF-8>
With arguments dst_encoding
and src_encoding
given, interprets self
using src_encoding
, encodes the new string using dst_encoding
:
s = "Ruby\x99" t = s.encode('UTF-8', 'Windows-1252') # => "Ruby™" t.encoding # => #<Encoding:UTF-8>
Optional keyword arguments enc_opts
specify encoding options; see Encoding Options.
Please note that, unless invalid: :replace
option is given, conversion from an encoding enc
to the same encoding enc
(independent of whether enc
is given explicitly or implicitly) is a no-op, i.e. the string is simply copied without any changes, and no exceptions are raised, even if there are invalid bytes.
Like encode
, but applies encoding changes to self
; returns self
.
Returns self
if self
is a String
, or self
converted to a String
if self
is a subclass of String
.
Changes the encoding of self
to encoding
, which may be a string encoding name or an Encoding
object; returns self:
s = 'łał' s.bytes # => [197, 130, 97, 197, 130] s.encoding # => #<Encoding:UTF-8> s.force_encoding('ascii') # => "\xC5\x82a\xC5\x82" s.encoding # => #<Encoding:US-ASCII>
Does not change the underlying bytes:
s.bytes # => [197, 130, 97, 197, 130]
Makes the change even if the given encoding
is invalid for self
(as is the change above):
s.valid_encoding? # => false s.force_encoding(Encoding::UTF_8) # => "łał" s.valid_encoding? # => true
Returns true
if self
is encoded correctly, false
otherwise:
"\xc2\xa1".force_encoding(Encoding::UTF_8).valid_encoding? # => true "\xc2".force_encoding(Encoding::UTF_8).valid_encoding? # => false "\x80".force_encoding(Encoding::UTF_8).valid_encoding? # => false
Like String#tr
, but also squeezes the modified portions of the translated string; returns a new string (translated and squeezed).
'hello'.tr_s('l', 'r') #=> "hero" 'hello'.tr_s('el', '-') #=> "h-o" 'hello'.tr_s('el', 'hx') #=> "hhxo"
Related: String#squeeze
.
If object
is a String
object, returns object
.
Otherwise if object
responds to :to_str
, calls object.to_str
and returns the result.
Returns nil
if object
does not respond to :to_str
.
Raises an exception unless object.to_str
returns a String
object.
Replaces the contents of self
with the contents of other_string
:
s = 'foo' # => "foo" s.replace('bar') # => "bar"
Returns an array of the grapheme clusters in self
(see Unicode Grapheme Cluster Boundaries):
s = "\u0061\u0308-pqr-\u0062\u0308-xyz-\u0063\u0308" # => "ä-pqr-b̈-xyz-c̈" s.grapheme_clusters # => ["ä", "-", "p", "q", "r", "-", "b̈", "-", "x", "y", "z", "-", "c̈"]
Returns whether self
starts with any of the given string_or_regexp
.
Matches patterns against the beginning of self
. For each given string_or_regexp
, the pattern is:
string_or_regexp
itself, if it is a Regexp
.
Regexp.quote(string_or_regexp)
, if string_or_regexp
is a string.
Returns true
if any pattern matches the beginning, false
otherwise:
'hello'.start_with?('hell') # => true 'hello'.start_with?(/H/i) # => true 'hello'.start_with?('heaven', 'hell') # => true 'hello'.start_with?('heaven', 'paradise') # => false 'тест'.start_with?('т') # => true 'こんにちは'.start_with?('こ') # => true
Related: String#end_with?
.
Like String#tr_s
, but modifies self
in place. Returns self
if any changes were made, nil
otherwise.
Related: String#squeeze!
.
With a block given, forms the substrings (“lines”) that are the result of splitting self
at each occurrence of the given line separator line_sep
; passes each line to the block; returns self
:
s = <<~EOT This is the first line. This is line two. This is line four. This is line five. EOT s.each_line {|line| p line }
Output:
"This is the first line.\n" "This is line two.\n" "\n" "This is line four.\n" "This is line five.\n"
With a different line_sep
:
s.each_line(' is ') {|line| p line }
Output:
"This is " "the first line.\nThis is " "line two.\n\nThis is " "line four.\nThis is " "line five.\n"
With chomp
as true
, removes the trailing line_sep
from each line:
s.each_line(chomp: true) {|line| p line }
Output:
"This is the first line." "This is line two." "" "This is line four." "This is line five."
With an empty string as line_sep
, forms and passes “paragraphs” by splitting at each occurrence of two or more newlines:
s.each_line('') {|line| p line }
Output:
"This is the first line.\nThis is line two.\n\n" "This is line four.\nThis is line five.\n"
With no block given, returns an enumerator.