How to access individual characters in a string (as strings)?

M

m4dc4p

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!

Justin
 
H

Hal Fulton

m4dc4p said:
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
 
R

Robert Brook

What are you doing withe the strings next?

You might want to have a look at StringScanner.
 
S

Steve Litt

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
(e-mail address removed)
 
D

Dirk Meijer

------=_Part_36512_21113829.1136564878656
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

2006/1/6 said:
m4dc4p said:
"abc"[0]
# =3D> 97

"abc"[0,1] ???


i agree, this looks a lot better to me than "abc"[0..0] does, and i've
always used this.

------=_Part_36512_21113829.1136564878656--
 
C

Christer Nilsson

Just a suggestion:

class String
def chr(index)
self[index..index]
end
end

s = "Ruby"
t s.chr(0), "R"
t s.chr(3), "y"
t s.chr(4), ""
t s.chr(-1), "y"
t s.chr(-4), "R"
t s.chr(-5), nil

But nothing beats s[0]="R", it's much cleaner.

Christer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top