I'm embarrassed to ask such a seeminly simple question, but I can't
figure out how to "easily" get the individual characters/bytes out of a
string as strings. For example:
"abc"[0]
# => 97
"abc"[0..0]
# => "a"
The first form gives me the character as a Fixnum, but I want a string.
The second form gives me what I want but it seems ugly. Is there a
better way to get at a given character, *as a string* ?
I'm embarrassed to ask such a seeminly simple question, but I can't
figure out how to "easily" get the individual characters/bytes out of a
string as strings. For example:
"abc"[0]
# => 97
"abc"[0..0]
# => "a"
The first form gives me the character as a Fixnum, but I want a string.
The second form gives me what I want but it seems ugly. Is there a
better way to get at a given character, *as a string* ?
There's no good way. In future Ruby, you'll get strings anyway
instead of Fixnums.
I usually use the [n..n] approach, but you can also do something
like: str[n].chr if you like that better.
You can use a lot of the same methods on both Arrays and Strings.
SteveT
Steve Litt
slitt@troubleshooters.com
···
On Friday 06 January 2006 03:03 am, m4dc4p wrote:
I'm embarrassed to ask such a seeminly simple question, but I can't
figure out how to "easily" get the individual characters/bytes out of a
string as strings. For example:
"abc"[0]
# => 97
"abc"[0..0]
# => "a"
The first form gives me the character as a Fixnum, but I want a string.
The second form gives me what I want but it seems ugly. Is there a
better way to get at a given character, *as a string* ?