Accessing Substrings


(For all intents and purposes, this project is dead, but let us just call it "Deferred Indefinitely". Shall we?)

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"
  ==>"this is an example"
value = string[2..6]
  ==>"is is"
string[11..-1] = "elephant"; string
  ==>"this is an elephant"
exit

or, for faster read-only access: 5-byte string, skip 3, 2 8-byte strings, then the rest

string = "12345abc1234567812345678blahblahblah"
  ==>"12345abc1234567812345678blahblahblah"
leading, s1, s2, trailing = string.unpack('A5 x3 A8 A8 A**')
  ==>["12345", "12345678", "12345678", "blahblahblah"]
exit

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:

FIX: this is not accurate according to RProg.

Related

Status: In Progress