# Getting character(s) of a string at specified positions

**URL:** <https://www.ruby-forum.com/t/getting-character-s-of-a-string-at-specified-positions/48952>\
**Category:** Ruby\
**Created:** [November 27, 2005, 7:01pm UTC](https://www.ruby-forum.com/t/getting-character-s-of-a-string-at-specified-positions/48952 "2005-11-27T19:01:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![theguy0000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@theguy0000](https://www.ruby-forum.com/u/theguy0000)\
**Post date:** [November 27, 2005, 7:01pm UTC](https://www.ruby-forum.com/t/getting-character-s-of-a-string-at-specified-positions/48952/1 "2005-11-27T19:01:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![theguy0000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@theguy0000](https://www.ruby-forum.com/u/theguy0000)\
**Post date:** [November 27, 2005, 7:05pm UTC](https://www.ruby-forum.com/t/getting-character-s-of-a-string-at-specified-positions/48952/2 "2005-11-27T19:05:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![theguy0000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@theguy0000](https://www.ruby-forum.com/u/theguy0000)\
**Post date:** [November 27, 2005, 7:09pm UTC](https://www.ruby-forum.com/t/getting-character-s-of-a-string-at-specified-positions/48952/3 "2005-11-27T19:09:34Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![theguy0000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@theguy0000](https://www.ruby-forum.com/u/theguy0000)\
**Post date:** [November 28, 2005, 3:21pm UTC](https://www.ruby-forum.com/t/getting-character-s-of-a-string-at-specified-positions/48952/4 "2005-11-28T15:21:07Z")

</div>

On 11/27/05, Alex F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> or
> 
> myString[4, 1]

or

myString[4…4]

or

myString[4…5]

-austin
