Get last character in string
Asked Answered
S

8

39

I want to get the last character in a string MY WAY - 1) Get last index 2) Get character at last index, as a STRING. After that I will compare the string with another, but I won't include that part of code here. I tried the code below and I get a strange number instead. I am using ruby 1.8.7.

Why is this happening and how do I do it ?

line = "abc;"
last_index = line.length-1
puts "last index = #{last_index}"
last_char = line[last_index]
puts last_char

Output-

last index = 3
59

Ruby docs told me that array slicing works this way -

a = "hello there"
a[1] #=> "e"

But, in my code it does not.

Stigmatism answered 14/1, 2015 at 6:51 Comment(3)
This seems so unintuitive to me. In Java and C#, this never happens.Stigmatism
Just use line[-1]. I’m not sure why everyone’s answering with line[-1, 1].Prier
Also, your code kinda works, the 'strange number' you are seeing is ; ASCII code. Every characters has a corresponding ascii code ( asciitable.com). You can use for conversationlast_char.chr, it should output ;.Mikimikihisa
E
75

UPDATE: I keep getting constant up votes on this, hence the edit. Using [-1, 1] is correct, however a better looking solution would be using just [-1]. Check Oleg Pischicov's answer.

line[-1]
# => "c"

Original Answer

In ruby you can use [-1, 1] to get last char of a string. Here:

line = "abc;"
# => "abc;"
line[-1, 1]
# => ";"

teststr = "some text"
# => "some text"
teststr[-1, 1]
# => "t"

Explanation: 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 String#slice as in OP's example: (will work only on ruby 1.9 onwards as explained in Yu Hau's answer)

line.slice(line.length - 1)
# => ";"
teststr.slice(teststr.length - 1)
# => "t"

Let's go nuts!!!

teststr.split('').last
# => "t"
teststr.split(//)[-1]
# => "t"
teststr.chars.last
# => "t"
teststr.scan(/.$/)[0]
# => "t"
teststr[/.$/]
# => "t"
teststr[teststr.length-1]
# => "t"
Epexegesis answered 14/1, 2015 at 6:55 Comment(3)
Thanks. But, can you also show me a solution which uses my logic ?Stigmatism
line.slice(line.length - 1) - Also gives the same output I was getting. It looks like this is a problem with ruby version < 1.9.x. Also read Yu Hao's answer.Stigmatism
yeah I read that. Sorry I was on ruby2.0 so could not validate your example.Epexegesis
M
46

Just use "-1" index:

a = "hello there"

a[-1] #=> "e"

It's the simplest solution.

Merengue answered 14/1, 2015 at 13:55 Comment(1)
This is much simpler than others.Ambrosia
C
10

If you are using Rails, then apply the method #last to your string, like this:

"abc".last
# => c
Canaan answered 15/2, 2016 at 19:0 Comment(1)
I just found out that chaining last on last does not work. it's not cool even if it would be dirty code doing this I believe. I like your post and benchmarking it just found that deception about the method lastKauppi
P
6

You can use a[-1, 1] to get the last character.

You get unexpected result because the return value of String#[] changed. You are using Ruby 1.8.7 while referring the the document of Ruby 2.0

Prior to Ruby 1.9, it returns an integer character code. Since Ruby 1.9, it returns the character itself.

String#[] in Ruby 1.8.7:

str[fixnum] => fixnum or nil

String#[] in Ruby 2.0:

str[index] → new_str or nil
Phosphorus answered 14/1, 2015 at 6:57 Comment(1)
I had a feeling this funky behavior had something to with my old ruby version. Thanks.Stigmatism
V
2

In ruby you can use something like this:

ending = str[-n..-1] || str

this return last n characters

Veg answered 14/1, 2015 at 7:35 Comment(0)
K
1

Using Rails library, I would call the method #last as the string is an array. Mostly because it's more verbose..

To get the last character.

"hello there".last() #=> "e"

To get the last 3 characters you can pass a number to #last.

"hello there".last(3) #=> "ere"
Kauppi answered 5/9, 2018 at 18:22 Comment(2)
This assumes you're using Rails. The question was about Ruby.Karinakarine
You are right. who does not using Ruby on Rails on every Ruby projects? probably many, but I think there are many better answers already without using rails library.Kauppi
A
0

Slice() method will do for you.

For Ex

 "hello".slice(-1)
 # => "o"

Thanks

Anticipate answered 14/1, 2015 at 7:5 Comment(3)
No. It does not help. It gives me 59Stigmatism
is that really a string..? what does it return if you print this "your string".class ..? String or something else..?Anticipate
This is the correct behavior for Ruby pre-1.9. The OP mentioned that he uses Ruby 1.8.7, an old, obsolete, deprecated, unmaintained version.Cataplexy
M
0

Your code kinda works, the 'strange number' you are seeing is ; ASCII code. Every characters has a corresponding ascii code ( https://www.asciitable.com/). You can use for conversationputs last_char.chr, it should output ;.

Mikimikihisa answered 18/7, 2018 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.