Forum: Ruby getting character(s) of a string at specified positions.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
theguy0000 (Guest)
on 2005-11-27 20:01
(Received via mailing list)
Ok, so lets say I have this:

myString = gets

I wanted to find what character is at position 5.

so if the use inputs

"How much wood would a wood-chuck chuck if a wood-chuck could chuck
wood"

I would find that the character there is "m".

How would I find the fifth character?
lists+rubytalk (Guest)
on 2005-11-27 20:05
(Received via mailing list)
Hi,

> "How much wood would a wood-chuck chuck if a wood-chuck could chuck wood"
>
> I would find that the character there is "m".
>
> How would I find the fifth character?

irb(main):001:0> a="How much wood would a wood-chuck chuck if a
wood-chuck could chuck wood"
=> "How much wood would a wood-chuck chuck if a wood-chuck could chuck
wood"
irb(main):002:0> a[4,1]
=> "m"

M.
alex (Guest)
on 2005-11-27 20:09
(Received via mailing list)
Matt Roth wrote:

> "How much wood would a wood-chuck chuck if a wood-chuck could chuck wood"
>
> I would find that the character there is "m".
>
> How would I find the fifth character?

myString[4].chr

or

myString[4, 1]
halostatue (Guest)
on 2005-11-28 16:21
(Received via mailing list)
On 11/27/05, Alex F. <removed_email_address@domain.invalid> wrote:
> or
>
> myString[4, 1]

or

myString[4..4]

or

myString[4...5]

-austin
This topic is locked and can not be replied to.