Problem
You want to access or modify just a portion of a string, not the whole thing. For instance, you’ve read a fixed-width record and want to extract the individual fields.
Solution
The indexed accessor methods lets you read from and write to substrings.
! string = “this is an example” ! value = string[2..6] ! string[11..-1] = “elephant”; string
or, for faster read-only access: 5-byte string, skip 3, 2 8-byte strings, then the rest
! string = “12345abc1234567812345678blahblahblah” ! leading, s1, s2, trailing = string.unpack(‘A5 x3 A8 A8 A**’)
Discussion
The string class provides a number of indexed accessors, that can both read and write to the string. You have the option of using a range for specifying the beginning and ending index of the string, Specifying the beginning index and the length, or just accessing a single character by index.
If you only need read-only access, unpack gives you a faster alternative, especially if you have more complex structured data.
Contrast
Perl has both a substr function and unpack. The substr function is analogous to the str[begin, length] form and also allows assignment. Ruby’s unpack is analogous to perl’s, except for:
- Ruby provides ‘w’, a BER compressed integer.
- Perl provides ‘m’, a base64 encoded string.
FIX: this is not accurate according to RProg.
Related
- TODO: a chapter on tokenizing
Status: In Progress