String methods
Case and Whitespace
.upcase
Convert all characters to uppercase.
"hello".upcase
→ "HELLO"
.downcase
Convert all characters to lowercase.
"HELLO".downcase
→ "hello"
.strip
Remove leading and trailing whitespace.
" hi ".strip
→ "hi"
.lstrip
Remove leading whitespace only.
.rstrip
Remove trailing whitespace only.
.chomp
Remove a trailing newline character. Useful after file_lines or user input.
.chop
Remove the last character unconditionally.
"hello!".chop
→ "hello"
Inspection & testing
length
Number of characters.
"Frankie".length
→ 7
.empty?
True if the string has zero characters.
"".empty?
→ true
.nil?
Always false for a string; useful in nil-safe chains with &.
.include?(n)
True if n appears anywhere in the string.
"Frankie".include?("n")"
→ true
.start_with?(n)
True if the string begins with n.
"Frankie".start_with?("F")
→ true
.end_with?(s)
True if the string ends with s.
"Frankie".end_with?("e")
→ true
.count(sub)
Count non-overlapping occurrences of sub.
"hello".count("l")
→ 2
Transformation
.reverse
Reverse the string character by character.
"Frankie".reverse
→ "eiknarF"
.replace(old, new)
Replace the first occurrence of old with new. Alias for sub().
"Franpie".replace("p","k")
→ "Frankie"
.delete(chars)
Remove all occurrences of any character in chars.
"hello".delete("l")
→ "heo"
.squeeze
Collapse runs of consecutive identical characters.
"aaabbbccc".squeeze
→ "abc"
.tr(from, to)
Translate characters — each character in from is replaced by the corresponding character in to.
"hello".tr("aeiou", "*")
→ "h*ll*"
.center(width, pad)
Center in a field of width, padded with pad.
"hi".center(10, "-")
→ "----hi----"
.ljust(width, pad)
Left-justify in a field of width.
"hi".ljust(10, "-")
→ hi--------
.rjust(width, pad)
Right-justify in a field of width.
"hi".rjust(10, "-")
→ --------hi
.encode
Convert string to a vector of byte values (UTF-8).
"hi".encode
→ [104, 105]
Splitting & joining
.split(sep)
Split on a separator string, return a Vector.
"a,b,c".split(",")
→ [a, b, c]
.join(sep)
Join a vector of strings with a separator. Called on a Vector but takes a string arg.
["a","b"].join("-")
→ a-b
.lines
Split into a vector of lines, stripping newline characters
"a,b,c".lines
→ [a,b,c]
.chars
Split into a vector of individual characters. Chains naturally into iterators.
puts "abc".chars
→ [a b c]
.bytes
Return a vector of integer byte values.
puts "abc".bytes
→ [97, 98, 99]
.each_char do |c|
Iterate over each character with a block.
"Frankie".each_char do |c|
puts c
end
→ F
r
a
n
k
i
e
.each_line do |l|
Iterate over each line with a block.
"Frankie is great!".each_line do |c|
c = c.delete("i")
puts c
end
→ Franke s great!
Slicing
[i]
Character at index i. Negative indices count from the end.
"hello"[-1]
→ "o"
[a..b]
Inclusive slice from index a to b.
"hello"[1..3]
→ "ell"
[a...b]
Exclusive slice — up to but not including b
"hello"[1...3]
→ "el"
template(str, hash)
{{key}} placeholder replacement.
puts template("{{name}} is awesome!", {name: "Frankie"})
→ Frankie is awesome!
sprintf(fmt, args)
C-style formatting.
puts sprintf("%-10s %5d %8.2f", "Frankie", 1, 3.14444)
→ Frankie 1 3.14
paste(a, b, sep)
Concatenate with separator.
puts paste("2026", "03", "14", sep: "/")
→ 2026/03/14