Google Groups Home
Help | Sign in
Getting last character from a string
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
draco draco  
View profile
 More options Feb 11 2006, 7:07 pm
Newsgroups: comp.lang.ruby
From: draco draco <thedr...@go2.pl>
Date: Sun, 12 Feb 2006 09:07:41 +0900
Local: Sat, Feb 11 2006 7:07 pm
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]

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
nurala...@aol.com  
View profile
(1 user)  More options Feb 11 2006, 7:12 pm
Newsgroups: comp.lang.ruby
From: Nurala...@aol.com
Date: Sun, 12 Feb 2006 09:12:02 +0900
Local: Sat, Feb 11 2006 7:12 pm
Subject: Re: Getting last character from a string

you can use

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Edward Gray II  
View profile
(3 users)  More options Feb 11 2006, 7:12 pm
Newsgroups: comp.lang.ruby
From: James Edward Gray II <ja...@grayproductions.net>
Date: Sun, 12 Feb 2006 09:12:35 +0900
Local: Sat, Feb 11 2006 7:12 pm
Subject: Re: Getting last character from a string
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Timothy Hunter  
View profile
 More options Feb 11 2006, 7:14 pm
Newsgroups: comp.lang.ruby
From: Timothy Hunter <cycli...@nc.rr.com>
Date: Sun, 12 Feb 2006 00:14:39 GMT
Local: Sat, Feb 11 2006 7:14 pm
Subject: Re: Getting last character from a string

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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Wilson Bilkovich  
View profile
(2 users)  More options Feb 11 2006, 7:16 pm
Newsgroups: comp.lang.ruby
From: Wilson Bilkovich <wils...@gmail.com>
Date: Sun, 12 Feb 2006 09:16:58 +0900
Local: Sat, Feb 11 2006 7:16 pm
Subject: Re: Getting last character from a string
On 2/11/06, draco draco <thedr...@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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gennady Bystritsky  
View profile
(1 user)  More options Feb 12 2006, 12:27 am
Newsgroups: comp.lang.ruby
From: "Gennady Bystritsky" <Gennady.Bystrit...@quest.com>
Date: Sun, 12 Feb 2006 14:27:09 +0900
Local: Sun, Feb 12 2006 12:27 am
Subject: Re: Getting last character from a string
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>


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
William James  
View profile
(2 users)  More options Feb 12 2006, 4:05 am
Newsgroups: comp.lang.ruby
From: "William James" <w_a_x_...@yahoo.com>
Date: 12 Feb 2006 01:05:22 -0800
Local: Sun, Feb 12 2006 4:05 am
Subject: Re: Getting last character from a string

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
draco draco  
View profile
 More options Feb 12 2006, 4:34 am
Newsgroups: comp.lang.ruby
From: draco draco <thedr...@go2.pl>
Date: Sun, 12 Feb 2006 18:34:50 +0900
Local: Sun, Feb 12 2006 4:34 am
Subject: Re: Getting last character from a string
Wow :)

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

Thanks a lot :)

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google