Returns a right-justified copy of self
.
If integer argument size
is greater than the size (in characters) of self
, returns a new string of length size
that is a copy of self
, right justified and padded on the left with pad_string
:
'hello'.rjust(10) # => " hello" 'hello '.rjust(10) # => " hello " 'hello'.rjust(10, 'ab') # => "ababahello" 'тест'.rjust(10) # => " тест" 'こんにちは'.rjust(10) # => " こんにちは"
If size
is not greater than the size of self
, returns a copy of self
:
'hello'.rjust(5, 'ab') # => "hello" 'hello'.rjust(1, 'ab') # => "hello"
Related: String#ljust
, String#center
.
Returns a copy of self
with each character specified by string selector
translated to the corresponding character in string replacements
. The correspondence is positional:
Each occurrence of the first character specified by selector
is translated to the first character in replacements
.
Each occurrence of the second character specified by selector
is translated to the second character in replacements
.
And so on.
Example:
'hello'.tr('el', 'ip') #=> "hippo"
If replacements
is shorter than selector
, it is implicitly padded with its own last character:
'hello'.tr('aeiou', '-') # => "h-ll-" 'hello'.tr('aeiou', 'AA-') # => "hAll-"
Arguments selector
and replacements
must be valid character selectors (see Character Selectors), and may use any of its valid forms, including negation, ranges, and escaping:
# Negation. 'hello'.tr('^aeiou', '-') # => "-e--o" # Ranges. 'ibm'.tr('b-z', 'a-z') # => "hal" # Escapes. 'hel^lo'.tr('\^aeiou', '-') # => "h-l-l-" # Escaped leading caret. 'i-b-m'.tr('b\-z', 'a-z') # => "ibabm" # Escaped embedded hyphen. 'foo\\bar'.tr('ab\\', 'XYZ') # => "fooZYXr" # Escaped backslash.
Like String#tr
, but modifies self
in place. Returns self
if any changes were made, nil
otherwise.
Returns a string containing the IP address representation in canonical form.
Splits str
into an array of tokens in the same way the UNIX Bourne shell does.
See Shellwords.shellsplit
for details.
Escapes str
so that it can be safely used in a Bourne shell command line.
See Shellwords.shellescape
for details.
Extracts data from self
.
If block
is not given, forming objects that become the elements of a new array, and returns that array. Otherwise, yields each object.
See Packed Data.
Like String#unpack
, but unpacks and returns only the first extracted object. See Packed Data.
Returns a new String object containing the given string
.
The options
are optional keyword options (see below).
With no argument given and keyword encoding
also not given, returns an empty string with the Encoding
ASCII-8BIT
:
s = String.new # => "" s.encoding # => #<Encoding:ASCII-8BIT>
With argument string
given and keyword option encoding
not given, returns a new string with the same encoding as string
:
s0 = 'foo'.encode(Encoding::UTF_16) s1 = String.new(s0) s1.encoding # => #<Encoding:UTF-16 (dummy)>
(Unlike String.new, a string literal like ''
or a string literal always has script encoding.)
With keyword option encoding
given, returns a string with the specified encoding; the encoding
may be an Encoding
object, an encoding name, or an encoding name alias:
String.new(encoding: Encoding::US_ASCII).encoding # => #<Encoding:US-ASCII> String.new('', encoding: Encoding::US_ASCII).encoding # => #<Encoding:US-ASCII> String.new('foo', encoding: Encoding::US_ASCII).encoding # => #<Encoding:US-ASCII> String.new('foo', encoding: 'US-ASCII').encoding # => #<Encoding:US-ASCII> String.new('foo', encoding: 'ASCII').encoding # => #<Encoding:US-ASCII>
The given encoding need not be valid for the string’s content, and its validity is not checked:
s = String.new('こんにちは', encoding: 'ascii') s.valid_encoding? # => false
But the given encoding
itself is checked:
String.new('foo', encoding: 'bar') # Raises ArgumentError.
With keyword option capacity
given, the given value is advisory only, and may or may not set the size of the internal buffer, which may in turn affect performance:
String.new('foo', capacity: 1) # Buffer size is at least 4 (includes terminal null byte). String.new('foo', capacity: 4096) # Buffer size is at least 4; # may be equal to, greater than, or less than 4096.
Compares self
and other_string
, returning:
-1 if other_string
is larger.
0 if the two are equal.
1 if other_string
is smaller.
nil
if the two are incomparable.
Examples:
'foo' <=> 'foo' # => 0 'foo' <=> 'food' # => -1 'food' <=> 'foo' # => 1 'FOO' <=> 'foo' # => -1 'foo' <=> 'FOO' # => 1 'foo' <=> 1 # => nil
Related: see Comparing.
Returns whether object
is equal to self
.
When object
is a string, returns whether object
has the same length and content as self
:
s = 'foo' s == 'foo' # => true s == 'food' # => false s == 'FOO' # => false
Returns false
if the two strings’ encodings are not compatible:
"\u{e4 f6 fc}".encode(Encoding::ISO_8859_1) == ("\u{c4 d6 dc}") # => false
When object
is not a string:
If object
responds to method to_str
, object == self
is called and its return value is returned.
If object
does not respond to to_str
, false
is returned.
Related: Comparing.
Returns whether object
is equal to self
.
When object
is a string, returns whether object
has the same length and content as self
:
s = 'foo' s == 'foo' # => true s == 'food' # => false s == 'FOO' # => false
Returns false
if the two strings’ encodings are not compatible:
"\u{e4 f6 fc}".encode(Encoding::ISO_8859_1) == ("\u{c4 d6 dc}") # => false
When object
is not a string:
If object
responds to method to_str
, object == self
is called and its return value is returned.
If object
does not respond to to_str
, false
is returned.
Related: Comparing.
Returns true
if object
has the same length and content; as self
; false
otherwise:
s = 'foo' s.eql?('foo') # => true s.eql?('food') # => false s.eql?('FOO') # => false
Returns false
if the two strings’ encodings are not compatible:
"\u{e4 f6 fc}".encode(Encoding::ISO_8859_1).eql?("\u{c4 d6 dc}") # => false
Returns the integer hash value for self
. The value is based on the length, content and encoding of self
.
Related: Object#hash
.
Compares self.downcase
and other_string.downcase
; returns:
-1 if other_string.downcase
is larger.
0 if the two are equal.
1 if other_string.downcase
is smaller.
nil
if the two are incomparable.
Examples:
'foo'.casecmp('foo') # => 0 'foo'.casecmp('food') # => -1 'food'.casecmp('foo') # => 1 'FOO'.casecmp('foo') # => 0 'foo'.casecmp('FOO') # => 0 'foo'.casecmp(1) # => nil
See Case Mapping.
Related: String#casecmp?
.
Returns true
if self
and other_string
are equal after Unicode case folding, otherwise false
:
'foo'.casecmp?('foo') # => true 'foo'.casecmp?('food') # => false 'food'.casecmp?('foo') # => false 'FOO'.casecmp?('foo') # => true 'foo'.casecmp?('FOO') # => true
Returns nil
if the two values are incomparable:
'foo'.casecmp?(1) # => nil
See Case Mapping.
Related: String#casecmp
.
Returns a new string containing other_string
concatenated to self
:
'Hello from ' + self.to_s # => "Hello from main"
Related: see Converting to New String.
Returns a new string containing n
copies of self
:
'Ho!' * 3 # => "Ho!Ho!Ho!" 'No!' * 0 # => ""
Related: see Converting to New String.
Returns the result of formatting object
into the format specifications contained in self
(see Format Specifications):
'%05d' % 123 # => "00123"
If self
contains multiple format specifications, object
must be an array or hash containing the objects to be formatted:
'%-5s: %016x' % [ 'ID', self.object_id ] # => "ID : 00002b054ec93168" 'foo = %{foo}' % {foo: 'bar'} # => "foo = bar" 'foo = %{foo}, baz = %{baz}' % {foo: 'bar', baz: 'bat'} # => "foo = bar, baz = bat"
Related: see Converting to New String.
Returns the substring of self
specified by the arguments. See examples at String Slices.
Related: see Converting to New String.
Replaces all, some, or none of the contents of self
; returns new_string
. See String Slices.
A few examples:
s = 'foo' s[2] = 'rtune' # => "rtune" s # => "fortune" s[1, 5] = 'init' # => "init" s # => "finite" s[3..4] = 'al' # => "al" s # => "finale" s[/e$/] = 'ly' # => "ly" s # => "finally" s['lly'] = 'ncial' # => "ncial" s # => "financial"
Related: see Modifying.
Returns the count of characters (not bytes) in self
:
'foo'.length # => 3 'тест'.length # => 4 'こんにちは'.length # => 5
Contrast with String#bytesize
:
'foo'.bytesize # => 3 'тест'.bytesize # => 8 'こんにちは'.bytesize # => 15
Returns the count of bytes (not characters) in self
:
'foo'.bytesize # => 3 'тест'.bytesize # => 8 'こんにちは'.bytesize # => 15
Contrast with String#length
:
'foo'.length # => 3 'тест'.length # => 4 'こんにちは'.length # => 5
Returns true
if the length of self
is zero, false
otherwise:
"hello".empty? # => false " ".empty? # => false "".empty? # => true