> 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:
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.
> -----Original Message----- > From: list-bou...@example.com > [mailto:list-bou...@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]
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]