Ruby String Replacement, Substitution and Insertion

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
Ruby String Concatenation and ComparisonRuby String Conversions


Purchase and download the full PDF and ePub editions of this Ruby eBook for only $8.99

Buy eBook


In this chapter we will look at some string manipulation techniques available in Ruby. Such techniques include string substitution, multiplication and insertion. We will also take a look at the Ruby chomp and chop methods.




Changing a Section of a String

Ruby allows part of a string to be modified through the use of the []= method. To use this method, simply pass through the string of characters to be replaced to the method and assign the new string. As is often the case, this is best explained through the use of an example:

myString = "Welcome to JavaScript!"

myString["JavaScript"]= "Ruby"

puts myString
=> "Welcome to Ruby!"

As you can see, we replaced the word "JavaScript" with "Ruby".

The []= method can also be passed an index representing the index at with the replacement is to take place. In this instance the single character at the specified location is replaced by the designated string:

myString = "Welcome to JavaScript!"
myString[10]= "Ruby"

puts myString
=> "Welcome toRubyJavaScript!"

Perhaps a more useful trick is to specify an index range to be replaced. For example we can replace the characters from index 8 through to index 20 inclusive:

myString = "Welcome to JavaScript!"
=> "Welcome to JavaScript!"

myString[8..20]= "Ruby"
=> "Ruby"

puts myString
=> "Welcome Ruby!"

Ruby String Substitution

The gsub and gsub! methods provide another quick and easy way of replacing a substring with another string. These methods take two arguments, the search string and the replacement string. The gsub method returns a modified string, leaving the original string unchanged, whereas the gsub! method directly modify the string object on which the method was called:

myString = "Welcome to PHP Essentials!"
=> "Welcome to PHP Essentials!"

myString.gsub("PHP", "Ruby")
=> "Welcome to Ruby Essentials!"

An entire string, as opposed to a substring, may be replaced using the replace method:

myString = "Welcome to PHP!"
=> "Welcome to PHP!"

myString.replace "Goodbye to PHP!"
=> "Goodbye to PHP!"

Repeating Ruby Strings

A string may be multiplied using the * method (not something I have ever needed to do myself, but the capability is there if you need it):

myString = "Is that an echo? "
=> "Is that an echo? "

myString * 3
=> "Is that an echo? Is that an echo? Is that an echo? "

Inserting Text into a Ruby String

So far in this chapter we have looked exclusively at changing the existing text contained in a Ruby string object. Another common requirement is to insert new text at a certain location in a string. This is achieved in Ruby using the insert method. The insert method takes as arguments index position into the string where the insertion is take place, followed by the string to be inserted


myString = "Paris in Spring"

myString.insert 8, " the"
=> "Paris in the Spring"

Ruby chomp and chop Methods

The purpose of the chop method is to remove the trailing character from a string:

myString = "Paris in the Spring!"
=> "Paris in the Spring!"

myString.chop
=> "Paris in the Spring"

Note that chop returns a modified string, leaving the original string object unchanged. Use chop! to have the change applied to the string object on which the method was called.

The chomp method removes record separators from a string. The record separator is defined by the $/ variable and is, by default, the new line character (\n). As with the chop method the chomp! variant of the method applies the change to string object on which the method is called:

myString = "Please keep\n off the\n grass"
=> "Please keep\n off the\n grass\n"

myString.chomp!
=> "Please keep\n off the\n grass"

Reversing the Characters in a String

The reverse method is used to reverse the contents of a string:

myString = "Paris in the Spring"
=> "Paris in the Spring"

myString.reverse
=> "gnirpS eht ni siraP"

Once again, not something I've ever needed to do in all my years as a developer, but you never know when you might need to do it.


Purchase and download the full PDF and ePub editions of this Ruby eBook for only $8.99

Buy eBook



PreviousTable of ContentsNext
Ruby String Concatenation and ComparisonRuby String Conversions