String Methods
String methods are the built-in operations Ruby provides for transforming and inspecting text — changing case, trimming whitespace, splitting, searching, and replacing — and they make text wrangling one of Ruby's great pleasures.
Learn String Methods in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
You'll use interpolation, upcase / strip / split / join , gsub , format , and the in-place bang methods like gsub! .
What You'll Learn in This Lesson
1️⃣ Interpolation, Case & Trimming
Inside double quotes, #{' ... '} drops any expression into a string. The case methods upcase , downcase , capitalize , and swapcase reshape letters, while strip removes surrounding whitespace and chomp removes a trailing newline.
2️⃣ Split, Join & Repeat
split turns a string into an array of pieces (on whitespace by default, or on any delimiter you give it), and join stitches an array back into a string. The * operator repeats a string — handy for separators and banners.
3️⃣ Search, Replace, Format & Bang Methods
Ask questions with include? , start_with? , and end_with? . Replace text with sub (first match) or gsub (all matches), build formatted output with format / % , and reach for a bang method like upcase! when you want to mutate the original string.
Your turn. Fill in each ___ blank with the right method, then run it.
Chain split , map , and join to build initials, then use gsub with string repetition to censor a word. Run with ruby strings.rb .
📋 Quick Reference — String Methods
Practice quiz
What does "ada lovelace".capitalize return?
- "Ada Lovelace"
- "ADA LOVELACE"
- "Ada lovelace"
- "ada lovelace"
Answer: "Ada lovelace". capitalize only uppercases the FIRST character of the whole string — it is not title-case.
What is the difference between gsub and sub?
- gsub replaces ALL matches, sub replaces only the FIRST
- No difference
- sub replaces all, gsub the first
- gsub mutates, sub does not
Answer: gsub replaces ALL matches, sub replaces only the FIRST. gsub (global) replaces every match; sub replaces only the first match.
What does "MixedCase".swapcase return?
- "mixedcase"
- "MIXEDCASE"
- "Mixedcase"
- "mIXEDcASE"
Answer: "mIXEDcASE". swapcase flips the case of every letter: upper becomes lower and vice versa.
What does "red,green,blue".split(",") return?
- "red green blue"
split breaks the string on the delimiter into an array of pieces.
What does a bang method like upcase! return when it makes NO change?
- nil
- The original string
- An empty string
- false
Answer: nil. A bang method returns the modified string, but returns nil when it changed nothing.
What does "ab" * 3 produce?
- "ab3"
The * operator repeats a string, so "ab" * 3 is "ababab".
What does format("Pi is %.2f", 3.14159) print?
- "Pi is 3.14159"
- "Pi is 3.14"
- "Pi is 3"
- "Pi is 3.1"
Answer: "Pi is 3.14". %.2f formats the float to two decimal places, giving "Pi is 3.14".
Which method removes surrounding whitespace from a string?
- chomp
- trim
- squeeze
- strip
Answer: strip. strip removes whitespace from both ends; chomp only removes a trailing newline.
Interpolation with #{...} works in which kind of string literal?
- Single quotes only
- Double quotes only
- Both single and double quotes
- Neither
Answer: Double quotes only. Interpolation only works in double-quoted strings; single quotes treat #{...} literally.
What does "%05d" % 42 produce?
- "42"
- "42000"
- "00042"
- "5"
Answer: "00042". %05d zero-pads the integer to a width of five characters, giving "00042".