- 2.1 Representing Ordinary Strings
- 2.2 Representing Strings with Alternate Notations
- 2.3 Using Here-Documents
- 2.4 Finding the Length of a String
- 2.5 Processing a Line at a Time
- 2.6 Processing a Byte at a Time
- 2.7 Performing Specialized String Comparisons
- 2.8 Tokenizing a String
- 2.9 Formatting a String
- 2.10 Using Strings As IO Objects
- 2.11 Controlling Uppercase and Lowercase
- 2.12 Accessing and Assigning Substrings
- 2.13 Substituting in Strings
- 2.14 Searching a String
- 2.15 Converting Between Characters and ASCII Codes
- 2.16 Implicit and Explicit Conversion
- 2.17 Appending an Item Onto a String
- 2.18 Removing Trailing Newlines and Other Characters
- 2.19 Trimming Whitespace from a String
- 2.20 Repeating Strings
- 2.21 Embedding Expressions Within Strings
- 2.22 Delayed Interpolation of Strings
- 2.23 Parsing Comma-Separated Data
- 2.24 Converting Strings to Numbers (Decimal and Otherwise)
- 2.25 Encoding and Decoding rot13 Text
- 2.26 Encrypting Strings
- 2.27 Compressing Strings
- 2.28 Counting Characters in Strings
- 2.29 Reversing a String
- 2.30 Removing Duplicate Characters
- 2.31 Removing Specific Characters
- 2.32 Printing Special Characters
- 2.33 Generating Successive Strings
- 2.34 Calculating a 32-Bit CRC
- 2.35 Calculating the MD5 Hash of a String
- 2.36 Calculating the Levenshtein Distance Between Two Strings
- 2.37 Encoding and Decoding base64 Strings
- 2.38 Encoding and Decoding Strings (uuencode/uudecode)
- 2.39 Expanding and Compressing Tab Characters
- 2.40 Wrapping Lines of Text
- 2.41 Conclusion
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 Fixnums, 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.