简体   繁体   中英

Get last character in string

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.

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"

Just use "-1" index:

a = "hello there"

a[-1] #=> "e"

It's the simplest solution.

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 

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

"abc".last
# => c

In ruby you can use something like this:

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

this return last n characters

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"

Slice() method will do for you.

For Ex

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

Thanks

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 puts last_char.chr , it should output ; .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM