← Back to Ruby Course | Chapter 3: Strings | Lesson 1 of 7

String Methods

String methods are built-in tools for text that can change case, measure length, or trim spaces. It is like having a toolbox ready for every word.

In this page:

  1. String Methods
Syntax
ruby
string.length
string.upcase
string.downcase
string.include?("text")
string.reverse

String Methods

Ruby strings come with a large set of built-in methods for transformation and inspection, like .upcase, .downcase, .reverse, .length, and .strip.

Most string methods return a new string rather than modifying the original, following Ruby's general preference for non-mutating operations. Methods ending in ! (like .upcase!) mutate the string in place instead.

Note: A method ending in ! (like upcase!) usually mutates the receiver in place -- the non-! version returns a new object instead.

Example: String Methods

markup
text = "  Hello, Ruby!  "

puts text.strip
puts text.strip.upcase
puts text.strip.length
puts text.strip.reverse
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting text.strip to change text, when it returns a new string unless you use strip!.
  2. Calling a bang method like upcase! and using the result, when it returns nil if nothing changed.
  3. Confusing length with the number of bytes, when Ruby counts characters (use bytesize for bytes).
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.