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.
8a04aefa8e7ee3c5c7efa253a8a4f5fd?d=identicon&s=25 theguy0000 (Guest)
on 2005-11-27 19: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?
7d89214d3177d8d9095cbc974a80dbba?d=identicon&s=25 lists+rubytalk (Guest)
on 2005-11-27 19: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.
669b7046f02e5dfc4bda4421f1069731?d=identicon&s=25 alex (Guest)
on 2005-11-27 19: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]
31ab75f7ddda241830659630746cdd3a?d=identicon&s=25 halostatue (Guest)
on 2005-11-28 15:21
(Received via mailing list)
On 11/27/05, Alex Fenton <alex@deleteme.pressure.to> wrote:
> or
>
> myString[4, 1]

or

myString[4..4]

or

myString[4...5]

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