Results for: "String# "

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:

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.

No documentation available

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 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)

Note that Ruby strings are null-terminated internally, so the internal buffer size will be one or more bytes larger than the requested capacity depending on the encoding.

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:

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(Encoding::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(Encoding::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.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:

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"

Returns a new String containing integer copies of self:

"Ho! " * 3 # => "Ho! Ho! Ho! "
"Ho! " * 0 # => ""

Returns the result of formatting object into the format specification self (see Kernel#sprintf for formatting details):

"%05d" % 123 # => "00123"

If self contains multiple substitutions, object must be an Array or Hash containing the values to be substituted:

"%-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"

Returns the substring of self specified by the arguments. See examples at String Slices.

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"

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
Search took: 5ms  ·  Total Results: 2656