Get last character in string
NickName:stack1 Ask DateTime:2015-01-14T14:51:48

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.

Copyright Notice:Content Author:「stack1」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/27937268/get-last-character-in-string

Answers
Aram 2015-01-14T07:35:02

In ruby you can use something like this:\n\nending = str[-n..-1] || str\n\n\nthis return last n characters",


Jade Hamel 2018-09-05T18:22:53

Using Rails library, I would call the method #last as the string is an array. Mostly because it's more verbose..\nTo get the last character.\n"hello there".last() #=> "e"\n\nTo get the last 3 characters you can pass a number to #last.\n"hello there".last(3) #=> "ere"\n",


Breen ho 2015-01-14T07:05:53

Slice() method will do for you.\n\nFor Ex \n\n \"hello\".slice(-1)\n # => \"o\"\n\n\nThanks",


shivam 2015-01-14T06:55:19

UPDATE:\nI 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.\n\nline[-1]\n# => \"c\"\n\n\nOriginal Answer\n\nIn ruby you can use [-1, 1] to get last char of a string. Here:\n\nline = \"abc;\"\n# => \"abc;\"\nline[-1, 1]\n# => \";\"\n\nteststr = \"some text\"\n# => \"some text\"\nteststr[-1, 1]\n# => \"t\"\n\n\nExplanation: \nStrings can take a negative index, which count backwards from the end\nof the String, and an length of how many characters you want (one in\nthis example). \n\nUsing String#slice as in OP's example: (will work only on ruby 1.9 onwards as explained in Yu Hau's answer)\n\nline.slice(line.length - 1)\n# => \";\"\nteststr.slice(teststr.length - 1)\n# => \"t\"\n\n\nLet's go nuts!!!\n\nteststr.split('').last\n# => \"t\"\nteststr.split(//)[-1]\n# => \"t\"\nteststr.chars.last\n# => \"t\"\nteststr.scan(/.$/)[0]\n# => \"t\"\nteststr[/.$/]\n# => \"t\"\nteststr[teststr.length-1]\n# => \"t\"\n",


Oleg Pischicov 2015-01-14T13:55:36

Just use \"-1\" index:\n\na = \"hello there\"\n\na[-1] #=> \"e\"\n\n\nIt's the simplest solution.",


victorhazbun 2016-02-15T19:00:14

If you are using Rails, then apply the method #last to your string, like this:\n\n\"abc\".last\n# => c\n",


Yu Hao 2015-01-14T06:57:33

You can use a[-1, 1] to get the last character.\n\nYou 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\n\nPrior to Ruby 1.9, it returns an integer character code. Since Ruby 1.9, it returns the character itself.\n\nString#[] in Ruby 1.8.7: \n\n\nstr[fixnum] => fixnum or nil\n\n\n\nString#[] in Ruby 2.0:\n\n\nstr[index] → new_str or nil\n\n",


More about “Get last character in string” related questions

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...

Show Detail

How to get the last character of a string?

How to get the last character of the string: "linto.yahoo.com." The last character of this string is "." How can I find this?

Show Detail

how do u get the last character of the last string in java?

I need to get the last character of the last input string. I wrote a piece of code that gets the last letter of the first string: public class LastCharacter { public static void main(String[] ...

Show Detail

Unable to get last character of string

** Code: ** String test = "32132SSS654"; char Lastchar = test[test.Length - 1]; Assert.IsTrue((Lastchar).Equals("4")); I am trying to get the last character from the string then doing assert but...

Show Detail

get last character in a string

I am new to developing windows phone apps. Now I am creating a text messenger app with T9 keyboard, I already made design like the buttons. Now what I want is how can I get the last character in a ...

Show Detail

Get the last character before a specific part of string in PHP

I would need a method to get the last character before a specific substring in a string. I need this to check if it's a SPACE before the specific substring, or not. I am looking for a function lik...

Show Detail

Last Index of Character in String

How do you get the last index of a character in a string in Lua? "/some/path/to/some/file.txt" How do I get the index of the last / in the above string?

Show Detail

Get last character of a string from a variable

I am having issues with a problem accomplished very easily in most languages but I can't seem to figure it out in batch. I want to extract the last character of a string. In pseudo code.. if var1.

Show Detail

How to get the last character of a string by coffeescript?

I have a string as below: str = "abcdefg123" I want to get the last character(3), how to make it by coffeescript? I am a newcomer to coffeescript, hope someone can help me, thanks in advance!

Show Detail

Check last character of a string

Is there a built-in POSIX equivalent for this bashism? my_string="Here is a string" last_character=${my_string: -1} I keep seeing things like this recommended, but they seem like hacks. last_cha...

Show Detail