Name
String — Character String class
Synopsis
String
is
one of Ruby’s basic datatypes, which contain arbitrary
sequences of bytes. String
can contain
\0
.
Included Module
Enumerable
,
Comparable
Class Method
String::new(
str
)
Creates a string.
Instance Methods
Methods of the
String
class ending in !
modify
their receiver and return a string if modification took place,
otherwise nil
. Methods without a
!
return a modified copy of the string.
~
s
Attempts to match pattern
s
against the$_
variable. This method is obsolete.s
%
arg
An abbreviated form of
sprintf(
s
,
arg...
)
. Multiple elements are specified using an array.s
*
n
Returns a string consisting of
s
copied end to endn
times.s
+
str
Returns a string with
str
concatenated tos
.s
<<
str
Concatenates
str
tos
.s
=~
x
Performs a regular expression match. If
x
is a string, it’s turned into aRegexp
object.s
[
n
]
Returns the code of the character at position
n
. Ifn
is negative, it’s counted as an offset from the end of the string.s
[
n
..
m
]
s
[
n
,
len
]
Returns a partial string.
"bar"[1..2] # => "ar" "bar"[1..-1] # => "ar" "bar"[-2..2] # => "ar" "bar"[-2..-1] # => "ar" "bar"[1,2] # => "ar" "bar"[-1, 1] # => "r"
s
[
n
]=
value
Replaces the
n
th element in the string withvalue
.value
may be a character code or string.s
[
n
..
m
]=
str
s
[
n
,
len
]=
str
Replaces a part of the string with
str
.s
.capitalize
s
.capitalize!
Returns a copy of
s
with the first character converted to uppercase and the remainder to lowercase."fooBar".capitalize # => "Foobar"
s
.center(
w
)
Returns a ...
Get Ruby in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.