Cover by Michael Fitzgerald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

Manipulating Strings

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 of slice, []= is an alias of slice!, so anywhere you use []=, you can use slice!, 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; nil otherwise.

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

That's looking better.

If you specify a Fixnum (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 index 13, []= found the substring V and replaced it with IV.

You can use an offset and length (two Fixnums) 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 index 39, and went 8 characters 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 if Porsche! appears at the end of the line ($). If this is true, the call to []= exchanges Porsche! with Targa!.

The chomp and chop Methods

The chop (or chop!) method chops off the last character of a string, and the chomp (chomp!) method chomps off the record separator ($/)—usually just a newline—from a string. Consider the string joe, a limerick created as a here document:

joe = <<limerick
There once was a fellow named Joe
quite fond of Edgar Allen Poe
   He read with delight
   Nearly half the night
When his wife said "Get up!" he said "No."
limerick # => "There once was a fellow named Joe\nquite fond of Edgar Allen
Poe\n   He read with delight\n   Nearly half the night\nWhen his wife said \"Get up!\"
he said \"No.\"\n"

Apply chomp! to remove the last record separator (\n):

joe.chomp! # => "There once was a fellow named Joe\nquite
fond of Edgar Allen Poe\n   He read with delight\n   Nearly half the
night\nWhen his wife said \"Get up!\" he said \"No.\""

Now apply it again, and chomp! returns nil without altering the string because there is no record separator at the end of the string:

joe.chomp! # => nil

chop, chomp's greedy twin, shows no mercy on the string, removing the last character (a quote) with abandon:

joe.chop! = "There once was a fellow named Joe\nquite fond of
Edgar Allen Poe\n   He read with delight\n   Nearly half the
night\nWhen his wife said \"Get up!\" he said \"No"

The delete Method

With delete or delete!, you can delete characters from a string:

"That's call folks!".delete "c" # => "That's all folks"

That looks easy, because there is only one occurrence of the letter c in the string, so you don't see any interesting side effects, as you would in the next example. Let's say you want to get rid of that extra l in alll:

"That's alll folks".delete "l" # => "That's a foks"

Oh, boy. It cleaned me out of all ls. I can't use delete the way I want, so how do I fix calll? What if I use two ls instead of one?

"That's alll folks".delete "ll" # => "That's a foks"

I got the same thing. (I knew I would.) That's because delete uses the intersection (what intersects or is the same in both) of its arguments to decide what part of the string to take out. The nifty thing about this, though, is you can also negate all or part of an argument with the caret (^), similar to its use in regular expressions:

"That's all folks".delete "abcdefghijklmnopqrstuvwxyz", "^ha" # => "haa"

The caret negates both the characters in the argument, not just the first one (you can do "^h^a", too, and get the same answer).

Substitute the Substring

Try gsub (or gsub!). This method replaces a substring (first argument) with a replacement string (second argument):

"That's alll folks".gsub "alll", "all" # => "That's all folks"

Or you might do it this way:

"That's alll folks".gsub "lll", "ll" # => "That's all folks"

The replace method replaces a string wholesale. Not just a substring, the whole thing.

call = "All hands on deck!"
call.replace "All feet on deck!" # => "All feet on deck!"

So why wouldn't you just do it this way?

call = "All hands on deck!"
call = "All feet on deck!"

Wouldn't you get the same result? Not exactly. When you use replace, call remains the same object, with the same object ID, but when you assign the string to call twice, the object and its ID will change. Just a subtlety you ought to know.

# same object
call = "All hands on deck!" # => "All hands on deck!"
call.object_id # => 1624370
call.replace "All feet on deck!" # => "All feet on deck!"
call.object_id # => 1624370

# different object
call = "All hands on deck!" # => "All hands on deck!"
call.object_id # => 1600420
call = "All feet on deck!" # => "All feet on deck!"
call.object_id # => 1009410

Turn It Around

To reverse the characters means to alter the characters so they read in the opposite direction. You can do this with the reverse method (or reverse! for permanent damage). Say you want to reverse the order of the English alphabet:

"abcdefghijklmnopqrstuvwxyz".reverse # => "zyxwvutsrqponmlkjihgfedcba"

Or, maybe you'd like to reverse a palindrome:

palindrome = "dennis sinned"
palindrome.reverse! # => "dennis sinned"
p palindrome

Not much harm done, even though reverse! changed the string in place. Think about that one for a while.

From a String to an Array

Conveniently, split converts a string to an array. The first call to split is without an argument:

"0123456789".split # => ["0123456789"]

That was easy, but what about splitting up all the individual values and converting them into elements? Do that with a regular expression (//) that cuts up the original string at the junction of characters.

"0123456789".split( // ) # => ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]

In the next example, the regular expression matches a comma and a space (/, /):

c_w = "George Jones, Conway Twitty, Lefty Frizzell, Ferlin Husky"
# => "George Jones, Conway Twitty, Lefty Frizzell, Ferlin Husky"
c_w.split(/, /) # => ["George Jones", "Conway Twitty",
"Lefty Frizzell", "Ferlin Husky"]

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required