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* ?
>
> Thanks for any and all help!

Lots of different ways, some of which are enumerated here:

http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Arrays

and here:

http://www.troubleshooters.cxm/codecorn/ruby/basictutorial.htm#_Strings

You can use a lot of the same methods on both Arrays and Strings.

SteveT

Steve Litt
http://www.troubleshooters.com
slitt / troubleshooters.com