Share this post!

Ruby String Methods (Ultimate Guide)

A string is just a sequence of characters.

Since strings are objects they have a lot of methods you can use to do things with them.

In this article you will find the most useful Ruby string methods with examples!

How to Get The String Length


What is String Interpolation?

String interpolation allows you to combine strings together:

What some people don’t know is that you can have actual code inside the interpolation.

Probably not something you want to do often, but here’s an example:

How to Extract a Substring

If you only want part of a string, instead of the whole string, then you can use a range to extract that substring.

Like this:

The first number is the starting index & the second number is the number of characters you want.

You can also use a range if you want to do something like “get all the characters but the last one”.


In this code the first index is the starting index & the second index is the ending index (inclusive).

How to Find Out If a String Contains Another String

What’s the easiest way to find if a string is included in another string?

The include? method:

You can also use the index method:

This method looks for partial words & instead of returning true or false it will give you the index where the start of this string is found.

In this example index is finding the “day” in “Today”.

If you want to find patterns (like all the words containing the word “day”) you are looking for regular expressions.

How to Pad a Ruby String

One way to pad a string is to use the rjust method with two arguments:

If you want to pad to the right you can use ljust:

Compare Strings Ignoring Case

Since string comparison is case-sensitive you want to make sure that both your strings are in the same case.

The common way to do that is to make both sides of the equation downcase or upcase.


There is also a casecmp? method that does a case insensitive comparison, but it’s rarely used, stick with the method above.

How to Trim a String & Remove White Space

When reading data from a file or a website you may find yourself with extra white space in your string.

You can remove that extra space with the strip method:

If you only want to remove the white space from one of the sides (left / right) you can use the lstrip & rstrip methods instead.

String Prefix & Suffix

You can use the start_with? method to check if a string starts with a specific prefix.

Here’s an example:

There’s also an end_with? method:

In addition, Ruby 2.5 introduced the delete_prefix & delete_suffix methods, which may be useful to you.

Here’s an example:

Convert a String to An Array of Characters

Taking a string & breaking it down into an array of characters is easy with the split method.


By default split will use a space as the separator character, but you can pass an argument into this method to specify a different separator.

Here’s how you can split a list of comma-separated values (CSV):

But if you are working with CSV data specifically you may want to consider using the CSV class from the standard library. This class can do things like reading column headers, so it makes things easier for you.

Convert an Array to a String

If you would like to take an array of strings & join these strings into a big string you can do that using the join method.


It’s also possible to pass an argument to join, this argument is the characters separator.


Convert an String Into An Integer

If you want to convert a string like "49" into the Integer 49 you can use the to_i method.


Notice that if you try this with a string that contains no numbers then you will get 0.


Check If A String Is A Number

Would you like to know if a string is made of only whole numbers?

You can do this:

Note: The match? method was introduced in Ruby 2.4, you can use match (without the question mark) on older versions.

This code uses a regular expression, let me translate it for you:

“From the start of the string (\A) check if there is an optional dash (-?, for negative numbers), then make sure there are some numbers in there (\d+) & nothing else until the end of the string (\Z).”

How to Append Characters

You can build up a big string from smaller strings by appending characters to an existing string. We also call this string concatenation.

Here’s how to do that using the << method:

Don’t use += for this because that will create a new string every time, which is not good for performance.

Iterate Over Characters Of a String in Ruby

Sometimes it’s useful to work with the individual characters of a string.

One way to do that is to use the each_char method:

You can also use the chars method to convert the string into an array of characters. Then you can use each on this array to iterate.


How to Convert a String to Upper or Lowercase in Ruby

If you would like to convert a string to all upper case you can use the upcase method.


And if you want to convert to lower case you can use the downcase method.


How to Create Multiline Strings

You can create multi-line strings in two different ways.

One is by using heredocs:

And another is by using %Q:

How to Replace Text Inside a String

If you want to replace some text inside a string you can use the gsub method.

Let’s replace the word “dogs” with “cats”:

Notice that gsub will return a new string, if you want to apply the changes to the original string you can use the gsub! method.

The gsub method also takes regular expressions as an argument so you can replace patterns instead of exact words.

Here’s an example:

This will replace all the numbers in the string with the number 5.

There is one more way to use this method, with a block:

How to Remove the Last Character From a String

If you are asking the user for some input (using the Kernel#gets method) then you will have a newline character (\n) at the end of your string, this prevents you from comparing the string directly.


The best way to remove that extra newline character (\n) is to use the chomp method.

It’s good to know that since Ruby 2.3 this method takes an optional argument that allows you to remove the characters you want to remove.


And if the character is not there it will return the original string.

How to Change String Encodings

Strings are stored as a sequence of bytes, they are turned into the characters that you can see based on their encoding.

For example, the number 65 in the ASCII encoding represents the letter “A”.

But there are also more complex encodings, like UTF-8, which allow you to represent characters from different languages (Chinese, etc.) & even emojis.

To find out the current encoding for a string you can use the encoding method.

When reading a file from disk or downloading some data from a website you may run into encoding problems.

You can often fix that problem by enforcing the encoding.

Like this:


You learned about many string methods, like join & split to break down strings into arrays, gsub to replace text inside strings & strip to trim out extra white space.

Since you may want to reference this page later make sure to bookmark it & share it with all your Ruby friends 🙂

Leave a Comment:

Maddox says last month

Nice post. Very helpful roundup and explanation of string methods.

    Jesus Castello says last month

    Thanks for reading! 🙂

Tom Connolly says last month

Thanks for your informative posts. Just the right size, too!

    Jesus Castello says last month

    Thanks for reading Tom! Let me know if there is any specific topic you would like me to cover 🙂

David C says last month

Really love your guides, simple, effective to the point. Keep up the good work!

    Jesus Castello says last month

    Thanks David! I’m glad you find them useful 🙂

Abdullah H says last month

In your post, you mentioned the method ‘casecmp?’ as a way to do case-insensitive string comparisons. However, you also recommended not to use it and instead prefer comparing up/downcased strings.

What is the downside to using ‘casecmp?’?

    Jesus Castello says last month

    Abdullah, I don’t think there is any real downside, it just doesn’t feel like something you would use in Ruby.

      Tara says last month

      I found your page because I was trying to figure out how to switch from downcase with the spaceship operator to casecmp, because rubocop blows up demanding casecmp, and we use rubocop for styling. casecmp is much faster when benchmarking, which is why I suspect the style guide is directing us that way. Meanwhile, I’m still not getting casecmp to work properly. I may need to disable rubocop there, but that’s not my first choice.

        Jesus Castello says last month

        Taking a look at the source code of Rubocop this is defined as a performance rule.

        The example given is this:

        I don’t like this at all, so I would avoid that unless you really need the performance boost.

        Tara says last month

        I ended up getting it to work just fine, and my company uses rubocop to process our pull requests, as did the last place it works. I suspect we’ll start seeing more of it. In any case, I’m glad to see your post! Every piece of info helps out here!

Simon says last month

Hi there! Great article, I am missing “<<~ curly doc”, maybe you can add it 🙂

Keep up the good work and thank you so much for this article!

    Jesus Castello says last month

    Hi Simon! Thanks for reading 🙂

Matthew Bell says last month

In your “How to Remove the Last Character From a String” topic the very lasg sentence says “And if the character is not there it will return the original array” I believe you meant string not array. Love the article though it didn’t occur to me that String#+ creates a new string object every time and String#<< does proper string concatenation to the original string! Cheers.

    Jesus Castello says last month

    Good catch! It’s fixed now, thank you.

Tushar Kulkarni says last month

Thanks. This was very informative and helpful.

    Jesus Castello says last month

    Thanks for reading 🙂

Add Your Reply