[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

accessing each character of a string

Boris Glawe

5/18/2005 11:39:00 PM

Hi,

my problem is obviously very basic, but how to I acces for example the third
character of the string "hello" ??

The [] method does not behave as expected, the each method returns the whole
string, the methods of Enumerable mixin don't help me...

This can't be that hard to solve!?

thanks Boris
4 Answers

Assaph Mehr

5/19/2005 12:01:00 AM

0


Boris Glawe wrote:
> Hi,
>
> my problem is obviously very basic, but how to I acces for example
the third
> character of the string "hello" ??
>
> The [] method does not behave as expected, the each method returns
the whole
> string, the methods of Enumerable mixin don't help me...
>
> This can't be that hard to solve!?
>
> thanks Boris

Just convert the Fixnum into a character:

irb(main):005:0> s = 'hello'
=> "hello"
irb(main):006:0> s[2]
=> 108
irb(main):007:0> s[2].chr
=> "l"

>From the docs:

--------------------------------------------------------------
String#[]
str[fixnum] => fixnum or nil
str[fixnum, fixnum] => new_str or nil
str[range] => new_str or nil
str[regexp] => new_str or nil
str[regexp, fixnum] => new_str or nil
str[other_str] => new_str or nil
str.slice(fixnum) => fixnum or nil
str.slice(fixnum, fixnum) => new_str or nil
str.slice(range) => new_str or nil
str.slice(regexp) => new_str or nil
str.slice(regexp, fixnum) => new_str or nil
str.slice(other_str) => new_str or nil
------------------------------------------------------------------------
Element Reference---If passed a single +Fixnum+, returns the code
of the character at that position. If passed two +Fixnum+ objects,
returns a substring starting at the offset given by the first, and
a length given by the second. If given a range, a substring
containing characters at offsets given by the range is returned.
In
all three cases, if an offset is negative, it is counted from the
end of _str_. Returns +nil+ if the initial offset falls outside
the
string, the length is negative, or the beginning of the range is
greater than the end.

If a +Regexp+ is supplied, the matching portion of _str_ is
returned. If a numeric parameter follows the regular expression,
that component of the +MatchData+ is returned instead. If a
+String+ is given, that string is returned if it occurs in _str_.
In both cases, +nil+ is returned if there is no match.

a = "hello there"
a[1] #=> 101
a[1,3] #=> "ell"
a[1..3] #=> "ell"
a[-3,2] #=> "er"
a[-4..-2] #=> "her"
a[12..-1] #=> nil
a[-2..-4] #=> ""
a[/[aeiou](.)\1/] #=> "ell"
a[/[aeiou](.)\1/, 0] #=> "ell"
a[/[aeiou](.)\1/, 1] #=> "l"
a[/[aeiou](.)\1/, 2] #=> nil
a["lo"] #=> "lo"
a["bye"] #=> nil

Gavin Kistner

5/19/2005 2:54:00 AM

0

On May 18, 2005, at 5:40 PM, Boris Glawe wrote:
> my problem is obviously very basic, but how to I acces for example
> the third character of the string "hello" ??

puts "hello"[2] #=> 108
puts "hello"[2..2] #=> "l"


Brian Schröder

5/19/2005 7:45:00 AM

0

On 19/05/05, Boris Glawe <boris@boris-glawe.de> wrote:
> Hi,
>
> my problem is obviously very basic, but how to I acces for example the third
> character of the string "hello" ??
>
> The [] method does not behave as expected, the each method returns the whole
> string, the methods of Enumerable mixin don't help me...
>
> This can't be that hard to solve!?
>
> thanks Boris
>

hello[2,1] => 'l'

regards,

Brian


--
http://ruby.brian-sch...

Stringed instrument chords: http://chordlist.brian-sch...


Boris Glawe

5/19/2005 11:29:00 AM

0

Assaph Mehr wrote:
> Boris Glawe wrote:
>
>>Hi,
>>
>>my problem is obviously very basic, but how to I acces for example
>
> the third
>
>>character of the string "hello" ??
>>
>>The [] method does not behave as expected, the each method returns
>
> the whole
>
>>string, the methods of Enumerable mixin don't help me...
>>
>>This can't be that hard to solve!?
>>
>>thanks Boris
>
>
> Just convert the Fixnum into a character:
>
> irb(main):005:0> s = 'hello'
> => "hello"
> irb(main):006:0> s[2]
> => 108
> irb(main):007:0> s[2].chr
> => "l"
>


thanks that's as smooth as I expected it from ruby :-)
Solutions like string[2..2], as they are shown in the documentation, are not
very nice!

greets Boris