String
A String is UTF-8 text. Strings are values that never change: every method returns a new
String. For how strings are written (quotes, escapes, interpolation with "#{...}", heredocs
and %q()), see Syntax.
Indexing and operators
Section titled “Indexing and operators”A String is indexed by character, as an Array is by item: s[0] is the first character,
s[-1] the last, and s[1..2] the substring from the second to the third. An index past the
end is an IndexError.
+ joins two Strings; anything else must be converted first or interpolated ("#{n} mm").
== and != compare by content, and <=> gives -1, 0 or 1 by byte order (nil against
a value that isn’t a String). < and > aren’t defined for Strings, and there is no *.
s = "héllo"puts s[1] # éputs s[-1] # oputs s[1..3] # éllputs "M" + "3" # M3puts("a" <=> "b") # -1String.new
Section titled “String.new”String.new(text = "")The String text, or an empty one. Returns: String.
Measuring
Section titled “Measuring”length
Section titled “length”s.lengths.sizeThe number of characters (not bytes): "héllo".length is 5. Returns: Number.
include?
Section titled “include?”s.include?(other)Whether other occurs in the String. Returns: Boolean.
starts_with?
Section titled “starts_with?”s.starts_with?(prefix)s.start_with?(prefix)Whether the String begins with prefix. Returns: Boolean.
ends_with?
Section titled “ends_with?”s.ends_with?(suffix)s.end_with?(suffix)Whether the String ends with suffix. Returns: Boolean.
name = "bracket_left.kup"puts name.length # 16puts name.include?("left") # trueputs name.starts_with?("brack") # trueputs name.end_with?(".kup") # trueChanging
Section titled “Changing”upcase
Section titled “upcase”s.upcases.downcaseThe String with its ASCII letters in upper case, or lower case. Other characters are kept as
they are: "ÉCOLE".downcase is "École". Returns: String.
reverse
Section titled “reverse”s.reverseThe characters in the opposite order. Returns: String.
replace
Section titled “replace”s.replace(target, replacement)The String with every occurrence of target replaced by replacement. An empty target is
found before each character and at the end, as in Ruby: "abc".replace("", "-") is
"-a-b-c-". (Ruby’s replace swaps the whole content; this one is closer to Ruby’s gsub
with a plain string.)
Returns: String.
s.trims.stripThe String without the spaces, tabs, carriage returns and line feeds at its ends.
Returns: String.
s.rjust(width, pad = " ")s.ljust(width, pad = " ")The String padded to width characters: rjust puts the padding before it (right-justifying
it), ljust after. The padding repeats pad, cut to fit. A String already width long or
longer is returned as it is.
| Parameter | Type | Default | Description |
|---|---|---|---|
width |
Number | — | The length to reach, in characters; a fraction is dropped. |
pad |
String | " " |
What to fill with; not empty. |
Returns: String.
Raises: ArgumentError for an empty pad or a width that isn’t finite; RangeError when
more than 1,000,000 characters would be added.
puts "7".rjust(3, "0") # 007puts "ab".ljust(5, "-.") # ab-.-puts "M3 bolt".replace("bolt", "screw") # M3 screwputs " lid \n".trim.inspect # "lid"puts "Bracket".upcase # BRACKETSplitting
Section titled “Splitting”s.split(delimiter)The pieces of the String between occurrences of delimiter, empty pieces included.
split("") gives the characters. The delimiter is required.
Returns: Array of Strings.
s.charsThe characters, each a String (the same as split("")). Returns: Array.
Raises: ValueError when the String isn’t valid UTF-8.
puts "a,b,,c".split(",").inspect # ["a", "b", "", "c"]puts "abc".chars.inspect # ["a", "b", "c"]Converting
Section titled “Converting”s.to_fThe String read as a number ("2.5", "-4", "1e3"), or nil when the whole String isn’t
one: "2.5mm".to_f and " 3".to_f are nil, where Ruby reads what it can.
Returns: Number or nil.
s.to_iThe String read as a whole number in decimal digits ("12", "-5"), or nil when the whole
String isn’t one: "3.5".to_i is nil.
Returns: Number or nil.
to_sym
Section titled “to_sym”s.to_symThe Symbol of that name: "m3".to_sym is :m3. Returns: Symbol.
s.to_sThe String itself. Returns: String.
puts "12".to_i + 1 # 13puts "2.5".to_f * 2 # 5puts "2.5mm".to_f.inspect # nilputs "m3".to_sym.inspect # :m3Formatting
Section titled “Formatting”format % valueformat % [value1, value2, ...]% fills in the directives of the format String from its right operand, as Ruby’s format
does: from an Array’s items in order, or from the one value. A directive is %, then optional
flags, a width and a precision (.2), and a conversion.
| Conversion | Writes |
|---|---|
%d, %i |
A whole number, the value truncated toward zero. |
%f |
A number with a fixed number of decimals (6 unless a precision is given). |
%e |
A number in scientific notation: 1.234568e+04. |
%g |
A number as %e when its exponent is below -4 or at least the precision (6 by default), else as %f; trailing zeros dropped unless #. |
%x, %o, %b |
A whole number in hexadecimal, octal or binary. |
%s |
The value as puts shows it (a precision cuts it to that many characters). |
%p |
The value as inspect shows it. |
%% |
A % sign. |
| Flag | Effect |
|---|---|
- |
Left-justifies within the width. |
0 |
Pads a number with zeros instead of spaces. |
+ |
Writes a + before a positive number. |
| space | Writes a space before a positive number. |
# |
Writes the base’s prefix (0x, 0, 0b), or keeps the point of a %f with no decimals. |
%f rounds the decimal digits the number prints with, halves away from zero, so
"%.2f" % 2.675 is "2.68" (C’s printf gives 2.67).
Returns: String.
Raises: ArgumentError when the format has more directives than there are arguments, or
fewer (Ruby ignores extra arguments), or an unknown directive (%c).
puts "%05.1f mm" % 3.14159 # 003.1 mmputs "%s x %d" % ["M3 bolt", 4] # M3 bolt x 4puts "%-6s|%6.2f|" % ["lid", 2.5] # lid | 2.50|puts "%#x %b" % [255, 5] # 0xff 101puts "%d%%" % 50 # 50%