Ruby Strings Cheat Guide for Beginners

Published: 2014-07-22

One of the first things I did in Ruby was manipulate a String. What you will quickly find is that Ruby offers a lot of quick and easy ways to work with strings to get the results you want. Here are a few string methods that I use in regular Rails development.

Note - Remember that inserting a variable into a string, requires the use of double quotes "

"The variable is: #{variable}" 
If you don't have a variable in the string, the convention is to use single quotes '

Tell me about the String

Use == to compare strings

'this string' == 'that string' # false

How long is the string

'this string'.length # 11

Check to see if a string is empty

''.empty? # true 

Does the string contain the string

'does the string have'.include? 'string' # true 

Does the string contain the regular expression (in this case the word 'long'), use if you need something more powerful than .include?

'the long string' =~ /long/ # 4 (true)

Modify the String

Note - Remember that a method with ! means that the variable will be modified directly.

Use + to join 2 strings

'this string' + 'that string' # 'this stringthat string' 

Convert string to all upper case, supports !

'this string'.upcase # "THIS STRING" 

Convert string to all lower case, supports !

'THIS STRING'.downcase # 'this string' 

Return the first 14 characters of a string, notice it starts with 0

'this is a long string'[0..13] # 'this is a long' 

Remove the newline/return character from a string (you need to use double quotes " for strings with special characters, supports !

"this string\n".chomp # "this string" 

Removes the whitespace at the start and end of the string (but not in the middle), supports !

'    roomy in here    '.strip # 'roomy in here' 

Removes the whitespace only at the start of the string, supports !

'    roomy in here    '.lstrip # 'roomy in here    ' 

Removes the whitespace only at the end of the string, supports !

'    roomy in here    '.rstrip # '    roomy in here' 

Convert string to an array by using space

'this is a long string'.split(' ') # ["this", "is", "a", "long", "string"]  

Find the regular expression

'this is a long string'.gsub(/long/, 'short') # this is a short string
and replace it with the string, in this case, replace long with short

Rails Specific

Rails has a lot of bonus extras for working with strings.

This will return true as there are no characters in the string

'  '.blank? # true 

Converts all words to leading Uppercase

'this is a big title'.titleize # 'This Is A Big Title' 

Returns the plural form of the string

'article'.pluralize # 'articles' 

Returns the singular form of the string

'articles'.singularize # 'article' 

Truncates at the given character count, replaces last 3 characters with '...'

'this is a long sentence that goes for a while'.truncate(15) # 'this is a lo...' 

Truncates at the given character count nearest the separator , replaces the last 3 characters with '...'

'this is a long sentence that goes for a while'.truncate(15, separator: ' ') # 'this is a...' 

Resources

Ruby String Class - The complete list of all String methods, best place to start when looking to work with strings.

Rails String Class - All the extra string methods available with Rails.


comments powered by Disqus