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.
Convert self
to ISO-2022-JP
Convert self
to EUC-JP
Convert self
to Shift_JIS
Convert self
to UTF-8
Convert self
to UTF-16
Convert self
to UTF-32
Convert self
to locale encoding
Returns whether self
‘s encoding is EUC-JP or not.
Returns whether self
‘s encoding is Shift_JIS or not.
Returns whether self
‘s encoding is ISO-2022-JP or not.
Returns whether self
‘s encoding is UTF-8 or not.
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
, forming objects that become the elements of a new array; returns that array. See Packed Data.
Like String#unpack
, but unpacks and returns only the first extracted object. See Packed Data.
Returns a new String that is a copy of string
.
With no arguments, returns the empty string with the Encoding
ASCII-8BIT
:
s = String.new s # => "" s.encoding # => #<Encoding:ASCII-8BIT>
With optional argument string
and no keyword arguments, returns a copy of string
with the same encoding:
String.new('foo') # => "foo" String.new('тест') # => "тест" String.new('こんにちは') # => "こんにちは"
(Unlike String.new, a string literal like ''
or a string literal always has script encoding.)
With optional keyword argument encoding
, returns a copy of string
with the specified encoding; the encoding
may be an Encoding
object, an encoding name, or an encoding name alias:
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 that 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 optional keyword argument capacity
, returns a copy of string
(or an empty string, if string
is not given); the given capacity
is advisory only, and may or may not set the size of the internal buffer, which may in turn affect performance:
String.new(capacity: 1) String.new('foo', capacity: 4096)
The string
, encoding
, and capacity
arguments may all be used together:
String.new('hello', encoding: 'UTF-8', capacity: 25)
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
Returns true
if object
has the same length and content; as self
; false
otherwise:
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("ISO-8859-1") == ("\u{c4 d6 dc}") # => false
If object
is not an instance of String but responds to to_str
, then the two strings are compared using object.==
.
Returns true
if object
has the same length and content; as self
; false
otherwise:
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("ISO-8859-1") == ("\u{c4 d6 dc}") # => false
If object
is not an instance of String but responds to to_str
, then the two strings are compared using object.==
.