String Methods

Up to now, I’ve used methods of the Regexp class when processing strings. In fact, pattern matching can go both ways because the String class has a few regular expression methods of its own. These include and match (so you can switch the order of the String and Regexp objects when matching), plus the scan method that iterates through a string looking for as many matches as possible. Each match is added to an array. Here, for example, I am looking for matches on the letters a, b, or c. The match method returns the first match (“a”) wrapped up in a MatchData object, but the scan method keeps scanning along the string and returns all the matches it finds as elements in an array:

match_scan.rb

TESTSTR = "abc is not cba"
puts( "
--match--" )
b = /[abc]/.match( TESTSTR )        #=> "a" (MatchData)
puts( "--scan--" )
a = TESTSTR.scan(/[abc]/)           #=> ["a", "b", "c", "c", "b", "a"]

The scan method may optionally be passed a block so that the elements of the array created by scan can be processed in some way:

a = TESTSTR.scan(/[abc]/){|c| print( c.upcase ) }     #=> ABCCBA

A number of other String methods can be used with regular expressions. One version of the String.slice method takes a regular expression as an argument and returns any matched substring, leaving the original (receiver) string unmodified. The String.slice! method (note the ! at the end) deletes the matched substring from the receiver string and returns the substring:

string_slice.rb

s = "def myMethod # a comment "

puts( s.slice( /m.*d/ ) )      #=> myMethod
puts( s )                      #=> def myMethod # a comment
puts( s.slice!( /m.*d/ ) )     #=> myMethod
puts( s )                      #=> def  # a comment

The split method splits a string into substrings, based on a pattern. The results (minus the pattern) are returned as an array:

string_ops.rb

s = "def myMethod # a comment"

p( s.split( /m.*d/ ) )   #=> ["def ", " # a comment"]
p( s.split( /s/ ) )     #=> ["def", "myMethod", "#", "a", "comment"]

You can also split on an empty pattern (//):

p( s.split( // ) )

In this case, an array of characters is returned:

["d", "e", "f", " ", "m", "y", "M", "e", "t", "h", "o", "d", " ", "#", " ",
"a", " ", "c", "o", "m", "m", "e", "n", "t"]

You can use the sub method to match a regular expression and replace its first occurrence with a string. If no match is made, the string is returned unchanged:

s = "def myMethod # a comment"
s2 = "The cat sat on the mat"
p( s.sub( /m.*d/, "yourFunction" ) )  #=> "def yourFunction # a comment"
p( s2.sub( /at/, "aterpillar" ) )     #=> "The caterpillar sat on the mat"

The sub! method works like sub but modifies the original (receiver) string. Alternatively, you can use the gsub method (or gsub! to modify the receiver) to substitute all occurrences of the pattern with a string:

p( s2.gsub( /at/, "aterpillar" ) )
        #=> "The caterpillar saterpillar on the materpillar"
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.93.141