Flylib.com

Books Software

 
 
 

Section 2.11. Controlling Uppercase and Lowercase


2.10. Using Strings As IO Objects

Besides sprintf and scanf , there is another way to fake input/output to a stringthe StringIO class.

Because this is a very IO -like object, we cover it in a later chapter. See section 10.1.24, "Treating a String As a File."



2.11. Controlling Uppercase and Lowercase

Ruby's String class offers a rich set of methods for controlling case. This section offers an overview of these.

The downcase method converts a string to all lowercase. Likewise upcase converts it to all uppercase:

s1 = "Boston Tea Party"
s2 = s1.downcase             # "boston tea party"
s3 = s2.upcase               # "BOSTON TEA PARTY"


The capitalize method capitalizes the first character of a string while forcing all the remaining characters to lowercase:

s4 = s1.capitalize           # "Boston tea party"
s5 = s2.capitalize           # "Boston tea party"
s6 = s3.capitalize           # "Boston tea party"


The swapcase method exchanges the case of each letter in a string:

s7 = "THIS IS AN ex-parrot."
s8 = s7.swapcase             # "this is an EX-PARROT."


As of Ruby 1.8, there is a casecmp method which acts like the default <=> method but ignores case:

n1 = "abc".casecmp("xyz")    # -1
n2 = "abc".casecmp("XYZ")    # -1
n3 = "ABC".casecmp("xyz")    # -1
n4 = "ABC".casecmp("abc")    # 0
n5 = "xyz".casecmp("abc")    # 1


Each of these has its in-place equivalent ( upcase! , downcase! , capitalize! , swapcase! ).

There are no built-in methods for detecting case, but this is easy to do with regular expressions, as shown in the following example:

if string =~ /[a-z]/
  puts "string contains lowercase characters"
end

if string =~ /[A-Z]/
  puts "string contains uppercase characters"
end

if string =~ /[A-Z]/ and string =~ /a-z/
  puts "string contains mixed case"
end

if string[0..0] =~ /[A-Z]/
  puts "string starts with a capital letter"
end


Note that all these methods ignore locale.



2.12. Accessing and Assigning Substrings

In Ruby, substrings may be accessed in several different ways. Normally the bracket notation is used, as for an array, but the brackets may contain a pair of Fixnum s, a range, a regex, or a string. Each case is discussed in turn .

If a pair of Fixnum values is specified, they are treated as an offset and a length, and the corresponding substring is returned:

str = "Humpty Dumpty"
sub1 = str[7,4]         # "Dump"
sub2 = str[7,99]        # "Dumpty" (overrunning is OK)
sub3 = str[10,-4]       # nil (length is negative)


It is important to remember that these are an offset and a length (number of characters ), not beginning and ending offsets.

A negative index counts backward from the end of the string. In this case, the index is one-based, not zero-based . The length is still added in the forward direction:

str1 = "Alice"
sub1 = str1[-3,3]   # "ice"
str2 = "Through the Looking-Glass"
sub3 = str2[-13,4]  # "Look"


A range may be specified. In this case, the range is taken as a range of indices into the string. Ranges may have negative numbers , but the numerically lower number must still be first in the range. If the range is "backward" or if the initial value is outside the string, nil is returned:

str = "Winston Churchill"
sub1 = str[8..13]    # "Church"
sub2 = str[-4..-1]   # "hill"
sub3 = str[-1..-4]   # nil
sub4 = str[25..30]   # nil


If a regular expression is specified, the string matching that pattern will be returned. If there is no match, nil will be returned:

str = "Alistair Cooke"
sub1 = str[/l..t/]   # "list"
sub2 = str[/s.*r/]   # "stair"
sub3 = str[/foo/]    # nil


If a string is specified, that string will be returned if it appears as a substring (or nil if it does not):

str = "theater"
sub1 = str["heat"]  # "heat"
sub2 = str["eat"]   # "eat"
sub3 = str["ate"]   # "ate"
sub4 = str["beat"]  # nil
sub5 = str["cheat"] # nil


Finally, in the trivial case, a single Fixnum as index will yield an ASCII code (or nil if out of range):

str = "Aaron Burr"
ch1 = str[0]     # 65
ch1 = str[1]     # 97
ch3 = str[99]    # nil


It is important to realize that the notations described here will serve for assigning values as well as for accessing them:

str1 = "Humpty Dumpty"
str1[7,4] = "Moriar"     # "Humpty Moriarty"

str2 = "Alice"
str2[-3,3] = "exandra"   # "Alexandra"

str3 = "Through the Looking-Glass"
str3[-13,13]  = "Mirror" # "Through the Mirror"

str4 = "Winston Churchill"
str4[8..13] = "H"        # "Winston Hill"

str5 = "Alistair Cooke"
str5[/e$/] ="ie Monster" # "Alistair Cookie Monster"

str6 = "theater"
str6["er"] = "re"        # "theatre"

str7 = "Aaron Burr"
str7[0] = 66             # "Baron Burr"


Assigning to an expression evaluating to nil will have no effect.