Difference between revisions of "Ruby String Replacement, Substitution and Insertion"

From Techotopia
Jump to: navigation, search
(New page: 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 a...)
 
(Repeating Ruby Strings)
Line 63: Line 63:
 
== Repeating Ruby Strings ==
 
== 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 is you need it):
+
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):
  
 
<pre>
 
<pre>
Line 72: Line 72:
 
=> "Is that an echo? Is that an echo? Is that an echo? "
 
=> "Is that an echo? Is that an echo? Is that an echo? "
 
</pre>
 
</pre>
 +
 +
== Inserting Text Into a Ruby String ==

Revision as of 16:33, 28 November 2007

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.


Contents


Changing a Section of a String

Ruby allows part of a string to be modified thorough 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