Difference between revisions of "Ruby String Concatenation and Comparison"

From Techotopia
Jump to: navigation, search
(New page: In the previous chapter (Ruby Strings - Creation and Basics) we looked at how to create a Ruby string object. In this chapter we will look at manipulating, comparing and concatenating...)
 
(Freezing a Ruby String)
Line 45: Line 45:
 
myString << "hello"
 
myString << "hello"
 
TypeError: can't modify frozen string
 
TypeError: can't modify frozen string
 +
</pre>
 +
 +
== Accessing String Elements ==
 +
 +
Fragments of a Ruby string can be accessed using the [] method of the ''String class''. One use for this method is to find if a particular sequence of characters exists in a string. If a match is found the sequence is returned, otherwise ''nil'' is returned:
 +
 +
<pre>
 +
myString = "Welcome to Ruby!"
 +
 +
myString["Ruby"]
 +
=> "Ruby"
 +
 +
myString["Perl"]
 +
=> nil
 +
</pre>
 +
 +
Pass an integer through to the [] method and the ASCII code of the character at that location in the string (starting at zero) will be returned. This can be converted back to a character using the ''chr'' method:
 +
 +
<pre>
 +
myString[3].chr
 +
=> "c"
 +
</pre>
 +
 +
You can also pass through a start position and a number of characters to extract a subsection of a string:
 +
 +
<pre>
 +
myString[11, 4]
 +
=> "Ruby"
 +
</pre>
 +
 +
You can also use a Range to specify a group of characters between start and end points:
 +
 +
<pre>
 +
myString[0..6]
 +
=> "Welcome"
 +
</pre>
 +
 +
The location of a matching substring can be obtained using the ''index'' method:
 +
 +
<pre>
 +
myString.index("Ruby")
 +
=> 11
 
</pre>
 
</pre>

Revision as of 20:55, 27 November 2007

In the previous chapter (Ruby Strings - Creation and Basics) we looked at how to create a Ruby string object. In this chapter we will look at manipulating, comparing and concatenating strings in Ruby.

Concatenating Strings in Ruby

If you've read any of the preceding chapters in this book you will have noticed that Ruby typically provides a number of different ways to achieve the same thing. Concatenating strings is certainly no exception to this rule.

Strings can be concatenated using the + method:

myString = "Welcome " + "to " + "Ruby!"
=> "Welcome to Ruby!"

In the interests of brevity, you can even omit the + signs:

 myString = "Welcome " "to " "Ruby!"
=> "Welcome to Ruby!"

If you aren't happy with the above options you can chain strings together using the << method:

myString = "Welcome " << "to " << "Ruby!"
=> "Welcome to Ruby!"

Still not enough choices for you. Well, how about using the concat method:

myString = "Welcome ".concat("to ").concat("Ruby!")
=> "Welcome to Ruby!"

Freezing a Ruby String

A string can be frozen after it has been created such that it cannot subsequently be altered. This is achieved using the freeze method of the String class:

myString = "Welcome " << "to " << "Ruby!"
=> "Welcome to Ruby!"

myString.freeze

myString << "hello"
TypeError: can't modify frozen string

Accessing String Elements

Fragments of a Ruby string can be accessed using the [] method of the String class. One use for this method is to find if a particular sequence of characters exists in a string. If a match is found the sequence is returned, otherwise nil is returned:

myString = "Welcome to Ruby!"

myString["Ruby"]
=> "Ruby"

myString["Perl"]
=> nil

Pass an integer through to the [] method and the ASCII code of the character at that location in the string (starting at zero) will be returned. This can be converted back to a character using the chr method:

myString[3].chr
=> "c"

You can also pass through a start position and a number of characters to extract a subsection of a string:

myString[11, 4]
=> "Ruby"

You can also use a Range to specify a group of characters between start and end points:

myString[0..6]
=> "Welcome"

The location of a matching substring can be obtained using the index method:

myString.index("Ruby")
=> 11