Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion Getting last character from a string
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
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
James Edward Gray II  
View profile  
 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.

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