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.
~sAttempts to match pattern
sagainst the$_variable. This method is obsolete.s%argAn abbreviated form of
sprintf(s,arg...). Multiple elements are specified using an array.s*nReturns a string consisting of
scopied end to endntimes.s+strReturns a string with
strconcatenated tos.s<<strConcatenates
strtos.s=~xPerforms a regular expression match. If
xis a string, it’s turned into aRegexpobject.s[n]Returns the code of the character at position
n. Ifnis 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]=valueReplaces the
nth element in the string withvalue.valuemay be a character code or string.s[n..m]=strs[n,len]=strReplaces a part of the string with
str.s.capitalizes.capitalize!Returns a copy of
swith the first character converted to uppercase and the remainder to lowercase."fooBar".capitalize # => "Foobar"
s.center(w)Returns a ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access