34

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.

toro2k
  • 18,388
  • 7
  • 58
  • 66
stack1
  • 894
  • 2
  • 11
  • 25
  • This seems so unintuitive to me. In Java and C#, this never happens. – stack1 Jan 14 '15 at 06:53
  • 10
    Just use `line[-1]`. I’m not sure why everyone’s answering with `line[-1, 1]`. – Ry- Jan 14 '15 at 07:24
  • Also, 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 conversation`last_char.chr`, it should output `;`. – Pierre Ferry Jul 18 '18 at 10:10

8 Answers8

63

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"
shivam
  • 14,497
  • 1
  • 47
  • 64
  • Thanks. But, can you also show me a solution which uses my logic ? – stack1 Jan 14 '15 at 06:56
  • 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. – stack1 Jan 14 '15 at 07:07
  • yeah I read that. Sorry I was on ruby2.0 so could not validate your example. – shivam Jan 14 '15 at 07:19
44

Just use "-1" index:

a = "hello there"

a[-1] #=> "e"

It's the simplest solution.

Oleg Pischicov
  • 602
  • 4
  • 10
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
Yu Hao
  • 111,229
  • 40
  • 211
  • 267
6

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

"abc".last
# => c
victorhazbun
  • 656
  • 8
  • 12
1

In ruby you can use something like this:

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

this return last n characters

Aram
  • 143
  • 3
  • 11
0

Slice() method will do for you.

For Ex

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

Thanks

shivam
  • 14,497
  • 1
  • 47
  • 64
Breen ho
  • 1,312
  • 10
  • 21
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 ;.

Pierre Ferry
  • 769
  • 7
  • 17
0

I would use the method #last as the string is an array.

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"
Jade Hamel
  • 1,149
  • 1
  • 14
  • 29