Home arrow Ruby-on-Rails arrow 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 / 6
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
(Page 1 of 4 )

Comparing Strings

Sometimes you need to test two strings to see if they are the same or not. You can do that with the == method. For example, you might want to test a string before printing something:

  print "What was the question again?" if question == ""

Also, here are two versions of the opening paragraph of Abraham Lincolnís Gettysburg Address, one from the so-called Hay manuscript, the other from the Nicolay (see http://www.loc.gov/exhibits/gadd/gadrft.html):

  hay = "Four score and seven years ago our fathers brought forth, upon this continent,
  a new nation, conceived in Liberty, and dedicated to the proposition that all men are
  created equal."

  nicolay = "Four score and seven years ago our fathers brought forth, upon this
  continent, a new nation, conceived in liberty, and dedicated to the proposition that
  "all men are created equal""

The strings are only slightly different (for example, Liberty is capitalized in the Hay version). Letís compare these strings:

  hay == nicolay # => false

The result isfalse, because they must match exactly. (Weíll let the historians figure out how to match them up.) You could also apply theeql?method and get the same results, thougheql?and==are slightly different:

  • ==returns true if two objects areStrings, false otherwise.
  • eql?returns true if two strings are equal in length and content, false otherwise.

Hereeql?returns false:

  hay.eql? nicolay # => false

Yet another way to compare strings is with the<=>method, commonly called the spaceship operator. It compares the character code values of the strings, returning-1(less than),0(equals), or1(greater than), depending on the comparison, which is case-sensitive:

  "a" <=> "a" # =>0
  "a" <=> 97.chr # => 0
  "a" <=> "b" # => -1
  "a" <=> "`" # => 1

A case-insensitive comparison is possible withcasecmp, which has the same possible results as<=> (-1,0,1) but doesnít care about case:

  "a" <=> "A" # => 1
  "a".casecmp "A" # => 0
  "ferlin husky".casecmp "Ferlin Husky" # => 0
  "Ferlin Husky".casecmp "Lefty Frizzell" # => -1


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-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials