Home arrow Ruby-on-Rails arrow Page 2 - Comparing and Manipulating Strings in Ruby
RUBY-ON-RAILS

Comparing and Manipulating Strings in Ruby


In this second part of a three-part series focusing on strings in Ruby, you will learn how to test two strings to see if they are the same, and more. It is excerpted from chapter four of Learning Ruby, written by Michael Fitzgerald (O'Reilly; ISBN: 0596529864). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 7
August 14, 2008
TABLE OF CONTENTS:
  1. · Comparing and Manipulating Strings in Ruby
  2. · Manipulating Strings
  3. · The chomp and chop Methods
  4. · The delete Method

print this article
SEARCH DEVARTICLES

Comparing and Manipulating Strings in Ruby - Manipulating Strings
(Page 2 of 4 )

Here’s a fun one to get started with. The * method repeats a string by an integer factor:

  "A horse! " * 2 # => "A horse! A horse! "

You can concatenate a string to the result:

  taf = "That's ".downcase * 3 + "all folks!" # => "that's that's that's all folks!"
  taf.capitalize # => "That's that's that's all folks!"

Inserting a String in a String

The insert method lets you insert another string at a given index in a string. For example, you can correct spelling:

  "Be carful.".insert 6, "e" # => "Be careful."

or add a word (plus a space):

  "Be careful!".insert 3, "very " # => "Be very careful!"

or even throw the*method in just to prove that you can:

  "Be careful!".insert 3, "very " * 5 # => "Be very very very very very careful!"

Changing All or Part of a String

You can alter all or part of a string, in place, with the
[]= method. (Like[], which is the counterpart ofslice,[]=is an alias ofslice!, so anywhere you use[]=, you can useslice!, with the same arguments.)

Given the following strings (some scoundrel has been editing our Shakespeare text):

  line = "A Porsche! a Porsche! my kingdom for a Porsche!"
  cite = "Act V, Scene V"
  speaker = "King Richard, 2007"

enter a string as the argument to[]=, and it will return the new, corrected string, if found;nilotherwise.

  speaker[", 2007"]= "III" # => "III"
  p speaker # => "King Richard III"

That’s looking better.

If you specify aFixnum(integer) as an index, it returns the corrected string you placed at the index location. (String lengths are automatically adjusted by Ruby if the replacement string is a different length than the original.)

  cite[13]= "IV" # => "IV"
  p cite # => "Act V, Scene IV"

At the index13,[]=found the substringVand replaced it withIV.

You can use an offset and length (twoFixnums) to tell[]=the index of the substring where you want to start, and then how many characters you want to retrieve:

  line[39,8]= "Porsche 911 Turbo!" # => "Porsche 911 Turbo!"
  p line # => "A Porsche! a Porsche! my kingdom for a Porsche 911 Turbo!"

You started at index39, and went8characters from there (inclusive).

You can also enter a range to indicate a range of characters you want to change. Include the last character with two dots (..):

  speaker[13..15]= "the Third" # => "the Third"
  p speaker # => "King Richard the Third"

You can also use regular expressions (see “Regular Expressions,” later in this chapter), as shown here:

  line[/Porsche!$/]= "Targa!" # => "Targa!"
  p line # => "A Porsche! a Porsche! my kingdom for a Targa!"

The regular expression/Porsche!$/ matches ifPorsche!appears at the end of the line ($). If this is true, the call to[]=exchangesPorsche!withTarga!.


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials