Hal E. Fulton
1/6/2006 8:06:00 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* ?
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.
Hal