Ruby String Methods and How to Use Them

Ruby String Methods and How to Use Them

Ultimate Guide to the Most Popular Ruby String Methods.

Every line of code you don't write is a line of code you don't have to maintain. Learning how to use methods effectively will help you reduce the lines of code you have to write and maintain.

Strings in Ruby are mutable by default. This means that its value can be changed without creating an entirely new value.

In this article, we are going to cover the most popular Ruby String Methods with examples.

How to Get The Length of a String in Ruby

The .length method and the size method both achieve the same purpose of determining the length of a string. Note that White spaces are included in the length of a string.

1. "Hello World".size
2. 11
1. "Ruby".length
2. 4

How to Capitalize a String in Ruby

The .capitalize method makes the first letter of a sentence in a string uppercase and the rest of the letters lowercase.

1. str = "HAPPY holiday!"
2. str.capitalize
3.
4. "Happy holiday!"

How to Uppercase a String in Ruby

We use the .upcase method to uppercase the string characters.

1. str = "Coding is fun."
2. puts str.upcase
3.
4. "CODING IS FUN."

How to Lowercase a String in Ruby

We use the .downcase method to lowercase all the string characters.

1. book = "The Well-Grounded Rubyist by David A. Black"
2. puts book.downcase
3.
4. "the well-grounded rubyist by david a.black"

How to Swapcase in Ruby

The .swapcase method converts uppercase characters in a string to lowercase and lowercase letters to uppercase.

1. str = "Swapcase Method in Action"
2. str.swapcase
3.
4. "sWAPCASE mETHOD IN aCTION"

How to Reverse the Order of Characters in a String in Ruby

We use the .reverse method to reverse the order of characters in a string.

1. name = "John Doe."
2. name.reverse
3.
4. ".eoD nhoJ"

How to Split a String into an Array in Ruby

The .split method takes a string and splits it into an array before returning the array.

1. str = "Which is your favorite movie?"
2. str.split
3.
4. ["Which", "is", "your", "favorite", "movie?"]

Unless a different separator is specified, the default .split method splits the string depending on whitespace. In the example below, I am splitting the string based on commas.

1. str = "London, New York, LA and Paris are great cities."
2. str.split(',')
3.
4. ["London", " New York", " LA and Paris are great cities."]

How to Convert a String to an Integer in Ruby

The .to_i method converts a string to an integer.

1. "22".to_1
2.
3. 22

How to Check Whether A String Is Empty in Ruby

A string is said to be empty if it has a length of 0 or is only made up of white spaces.

1. "  ".empty?
2. true

How to Interpolate a String in Ruby

String interpolation is the technique through which placeholder characters are substituted with variables (or strings in this case).

1. name = "Francis"
2. puts "My name is #{name}"
3.
4. "My name is Francis"

One can also run mathematical computation inside the interpolation.

1. puts "The sum is #{5 + 6}"
2. "The sum is 11"

How to Delete Leading and Trailing White Spaces in Ruby

The .strip method eliminates leading and trailing white spaces from strings, including tabs, newlines, and carriage returns (\t, \n, \r).

1. str = "    Hello World    "
2. str.strip
3.
4. "Hello World"

How to Concatenate a String in Ruby

There are 3 methods to concatenate two or more strings together in Ruby. These are:

The + method
1. str_1 = "Hello"
2. str_2 ="World!"
3. str_1 + str_2
4.
5. "HelloWorld!"
The << method
1. str_1 = "Hello"
2. str_2 ="World!"
3. str_1 << str_2
4.
5. "HelloWorld!"
The .concat method
1. str_1 = "20"
2. str_1.concat"22"
3. 
4. "2022"

How to Determine Whether a String is Contained in Another String in Ruby

The .include? method will return either true or false depending on whether the substring is contained in the string. Note that include? method is case sensitive.

1. str = "I am a fullstack developer"
2. str.include?("developer")
3.
4. true

How to Check the Index Number Position of a Character in Ruby

Instead of returning true or false, the .index method will return the index where the beginning of this string is found. Note that .index method is case sensitive.

1. str = "Ruby is my favorite language."
2. str.index("favorite")
3.
4. 11

In this example, .index is parsing through our str to find favorite.

How to Check How Many Times a Specific Character(s) is Found in a String in Ruby

We use the .count method to count how many times a specific character(s) is found in a string. Note that this method is case sensitive.

1. greeting = "Good morning"
2. puts greeting.count("o")
3.
4. 3
5.
6. puts greeting.count("morning")
7. 1

How to Get a Substring in Ruby

Substring is a subset or part of another string. One way of getting a substring is using index position of a character in a string.

1. str = "abcdef"
2.
3. str[0,3]
4. "abc"
5.
6. str[3,2]
7. "ef"

The fist number in the brackets denotes the starting index while the second number refers to the number of characters you want from the starting index.

You may also use range to perform things like "get all the characters except the last one."

1. str ="abcdef"
2. str[0..-2]
3.
4. "abcde"

The first index remains the initial index, but the second index is now the final index (inclusive). This -2 is the second-to-last character in the string, while -1 marks the end of the string.

Authors Note

These are some of the most often used string methods in most projects. There are a few additional string methods that aren't as commonly used, and I'll be updating the article as time goes by and I get more opportunities to interact with the methods. Thank you😊.