Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting last character from a string

773 views
Skip to first unread message

draco draco

unread,
Feb 11, 2006, 7:07:41 PM2/11/06
to
Hello
I'm new to Ruby. I've a simple question.
Is there a more readable way to get last character from string than this
one? :

x = "text"
last_char = x[x.length-1, x.length-1]

--
Posted via http://www.ruby-forum.com/.


Nura...@aol.com

unread,
Feb 11, 2006, 7:12:02 PM2/11/06
to
you can use

text="my unfinishing text with an unknown last character"
result=text[-1].chr
p result => r

James Edward Gray II

unread,
Feb 11, 2006, 7:12:35 PM2/11/06
to
On Feb 11, 2006, at 6:07 PM, draco draco wrote:

> Hello

Hello.

> I'm new to Ruby.

Welcome then.

> I've a simple question.
> Is there a more readable way to get last character from string than
> this
> one? :
>
> x = "text"
> last_char = x[x.length-1, x.length-1]

Strings can take a negative index, which count backwards from the end
of the String, and an length of how many characters you want (one in
this example). Using that:

"test"[-1, 1] # => "t"

Hope that helps.

James Edward Gray II


Timothy Hunter

unread,
Feb 11, 2006, 7:14:39 PM2/11/06
to
draco draco wrote:
> Hello
> I'm new to Ruby. I've a simple question.
> Is there a more readable way to get last character from string than this
> one? :
>
> x = "text"
> last_char = x[x.length-1, x.length-1]
>

last_char = x[-1,1]

A negative index counts from the right-hand end of the string.

Wilson Bilkovich

unread,
Feb 11, 2006, 7:16:58 PM2/11/06
to
On 2/11/06, draco draco <thed...@go2.pl> wrote:
> Hello
> I'm new to Ruby. I've a simple question.
> Is there a more readable way to get last character from string than this
> one? :
>
> x = "text"
> last_char = x[x.length-1, x.length-1]
>

You can do:
last_char = x[-1,1]
or # possibly too arcane
last_char = x.split('').last

The second has the advantage of being (I think) multibyte-character safe.


Gennady Bystritsky

unread,
Feb 12, 2006, 12:27:09 AM2/12/06
to
x[-1].chr

[tiger-ppc.gfbm:246]bystr> irb
irb(main):001:0> s = "abc"
=> "abc"
irb(main):002:0> s[-1]
=> 99
irb(main):003:0> s[-1].chr
=> "c"

irb(main):004:0>

> -----Original Message-----
> From: list-...@example.com
> [mailto:list-...@example.com] On Behalf Of draco draco
> Sent: Saturday, February 11, 2006 16:08
> To: ruby-talk ML
> Subject: Getting last character from a string
>
> Hello
> I'm new to Ruby. I've a simple question.
> Is there a more readable way to get last character from
> string than this one? :
>
> x = "text"
> last_char = x[x.length-1, x.length-1]
>

William James

unread,
Feb 12, 2006, 4:05:22 AM2/12/06
to
draco draco wrote:
> Hello
> I'm new to Ruby. I've a simple question.
> Is there a more readable way to get last character from string than this
> one? :
>
> x = "text"
> last_char = x[x.length-1, x.length-1]

"abc"[ -1..-1 ]

"abc".slice(-1,1)

"abc".slice(-1).chr

"abc"[ /.$/ ]

"abc".reverse[0,1]

"abc".split('').pop

class String
def last
self[-1,1]
end
end

"abc".last

draco draco

unread,
Feb 12, 2006, 4:34:50 AM2/12/06
to
Wow :)

x[-1,1] looks far more readable than x[x.length-1, x.length-1]

Thanks a lot :)

0 new messages