[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

jgbailey

1/5/2006 10:53:00 PM

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

6 Answers

Hal E. Fulton

1/6/2006 8:06:00 AM

0

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



pere.noel

1/6/2006 8:38:00 AM

0

m4dc4p <jgbailey@gmail.com> wrote:

> "abc"[0]
> # => 97

"abc"[0,1] ???
--
une bévue

robertbrook.com@gmail.com

1/6/2006 9:08:00 AM

0

What are you doing withe the strings next?

You might want to have a look at StringScanner.

Steve Litt

1/6/2006 2:53:00 PM

0

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.troublesh.../codecorn/ruby/basictutorial.h...

and here:

http://www.troubleshooters.cxm/codecorn/ruby/basictutorial.ht...

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

SteveT

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


Dirk Meijer

1/6/2006 4:28:00 PM

0

2006/1/6, Une bévue <pere.noel@laponie.com>:
>
> m4dc4p <jgbailey@gmail.com> wrote:
>
> > "abc"[0]
> > # => 97
>
> "abc"[0,1] ???


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

Christer Nilsson

1/6/2006 5:39:00 PM

0

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

--
Posted via http://www.ruby-....