# Getting single char from string -- Simpler solution?

**URL:** <https://www.ruby-forum.com/t/getting-single-char-from-string-simpler-solution/154107>\
**Category:** Ruby\
**Created:** [November 25, 2008, 1:51pm UTC](https://www.ruby-forum.com/t/getting-single-char-from-string-simpler-solution/154107 "2008-11-25T13:51:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [November 25, 2008, 1:51pm UTC](https://www.ruby-forum.com/t/getting-single-char-from-string-simpler-solution/154107/1 "2008-11-25T13:51:41Z")

</div>

Hi -

I’m trying to get a single char from string, but doing it like this  
return the ascii key:

irb(main):060:0\> “hello”[0]  
=\> 104

I know this could be accomplished like this:

irb(main):063:0\> “hello”.split(’’)[0]  
=\> “h”

I’m not too lazy to type, but I was wondering if there’s a simpler way I  
overlooked.

Thanks!

---

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [November 25, 2008, 1:54pm UTC](https://www.ruby-forum.com/t/getting-single-char-from-string-simpler-solution/154107/2 "2008-11-25T13:54:38Z")

</div>

Alle Tuesday 25 November 2008, sa 125 ha scritto:

> irb(main):063:0\> “hello”.split(’’)[0]  
> =\> “h”
> 
> I’m not too lazy to type, but I was wondering if there’s a simpler way I  
> overlooked.
> 
> Thanks!

“hello”[0,1]

“hello”[0…0]

“hello”[0].chr

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [November 25, 2008, 2:19pm UTC](https://www.ruby-forum.com/t/getting-single-char-from-string-simpler-solution/154107/3 "2008-11-25T14:19:16Z")

</div>

On Tue, Nov 25, 2008 at 10:49 AM, Stefano C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> “hello”[0…0]
> 
> “hello”[0].chr
> 
> I hope this helps
> 
> Stefano

Just complementing, this is now the default behavior in 1.9:

> > “hello”[0]  
> > =\> “h” # note no need for .chr

For 1.8, “hello”[0,1] sounds sounds like what you’d want in those  
situations.

Diogo

---

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [November 25, 2008, 2:48pm UTC](https://www.ruby-forum.com/t/getting-single-char-from-string-simpler-solution/154107/4 "2008-11-25T14:48:55Z")

</div>

Thanks guys, that’s exactly what I was looking for.
