# String functions

**URL:** <https://www.ruby-forum.com/t/string-functions/146455>\
**Category:** Ruby\
**Created:** [August 21, 2008, 10:48am UTC](https://www.ruby-forum.com/t/string-functions/146455 "2008-08-21T10:48:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [August 21, 2008, 10:48am UTC](https://www.ruby-forum.com/t/string-functions/146455/1 "2008-08-21T10:48:02Z")

</div>

I have string.  
In that i need to display the first element from String means Which  
Function Should I use?  
I Am Unable To Figure Out…

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [August 21, 2008, 10:52am UTC](https://www.ruby-forum.com/t/string-functions/146455/2 "2008-08-21T10:52:57Z")

</div>

Newb N. wrote:

> I have string.  
> In that i need to display the first element from String means Which  
> Function Should I use?  
> I Am Unable To Figure Out…

If you mean first letter :

name[0] #shows code  
name[0].chr #shows letter

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [August 21, 2008, 10:56am UTC](https://www.ruby-forum.com/t/string-functions/146455/3 "2008-08-21T10:56:04Z")

</div>

On Thursday 21 August 2008, Newb N. wrote:

> I have string.  
> In that i need to display the first element from String means Which  
> Function Should I use?  
> I Am Unable To Figure Out…

Try String#[] or String#slice, but since they return a number (the code  
of the  
first character) if you only specify an index, you’ll need to pass the  
index  
and a length of 1, like this:

“test string”[0,1]  
=\> “t”

For more information, see the documentation for the String#slice method  
(ri  
String#slice).

Stefano

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [August 21, 2008, 11:10am UTC](https://www.ruby-forum.com/t/string-functions/146455/4 "2008-08-21T11:10:07Z")

</div>

Newb N. schrieb:

> I have string.  
> In that i need to display the first element from String means Which  
> Function Should I use?  
> I Am Unable To Figure Out…

“ASDASDAS”[0] → 65

“ASDASDAS”[0].chr → “A”

Please add [RDoc Documentation](http://www.ruby-doc.org/core/) to your Favorites, look at  
Class “String” and you will find everything you need.

–  
Otto Software Partner GmbH

Jan P. (e-mail: removed\_email\_address@domain.invalid)

Tel. 0351/49723202, Fax: 0351/49723119  
01067 Dresden, Freiberger StraÃŸe 35 - AG Dresden, HRB 2475  
GeschÃ¤ftsfÃ¼hrer: Burkhard Arrenberg, Heinz A. Bade, Jens Gruhl
