ruby 获取字符串中的最后一个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27937268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get last character in string
提问by stack1
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.
我想以我的方式获取字符串中的最后一个字符 - 1)获取最后一个索引 2)获取最后一个索引处的字符,作为字符串。之后我会将字符串与另一个字符串进行比较,但我不会在此处包含该部分代码。我尝试了下面的代码,却得到了一个奇怪的数字。我正在使用 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 docstold me that array slicing works this way -
Ruby 文档告诉我数组切片是这样工作的 -
a = "hello there"
a[1] #=> "e"
But, in my code it does not.
但是,在我的代码中它没有。
回答by shivam
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.
更新:我一直在为此获得不断的投票,因此进行了编辑。使用[-1, 1]是正确的,但是更好看的解决方案是使用[-1]. 检查奥列格·皮斯基科夫的回答。
line[-1]
# => "c"
Original Answer
原答案
In ruby you can use [-1, 1]to get last char of a string. Here:
在 ruby 中,您可以使用[-1, 1]获取字符串的最后一个字符。这里:
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#sliceas in OP's example: (will work only on ruby 1.9 onwards as explained in Yu Hau's answer)
使用String#slice在OP的例子:(只能在Ruby 1.9的工作,俞厚的回答起担任解释)
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"
回答by Oleg Pischicov
Just use "-1" index:
只需使用“-1”索引:
a = "hello there"
a[-1] #=> "e"
It's the simplest solution.
这是最简单的解决方案。
回答by Yu Hao
You can use a[-1, 1]to get the last character.
您可以使用a[-1, 1]获取最后一个字符。
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
你得到意想不到的结果,因为返回值String#[]改变了。您在参考 Ruby 2.0 的文档时使用的是 Ruby 1.8.7
Prior to Ruby 1.9, it returns an integer character code. Since Ruby 1.9, it returns the character itself.
在 Ruby 1.9 之前,它返回一个整数字符代码。从 Ruby 1.9 开始,它返回字符本身。
str[fixnum] => fixnum or nil
str[fixnum] => fixnum or nil
str[index] → new_str or nil
str[index] → new_str or nil
回答by victorhazbun
If you are using Rails, then apply the method #last to your string, like this:
如果您使用 Rails,则将方法 #last 应用于您的字符串,如下所示:
"abc".last
# => c
回答by Charles Hamel
I would use the method #last as the string is an array.
我会使用 #last 方法,因为字符串是一个数组。
To get the last character.
获取最后一个字符。
"hello there".last() #=> "e"
To get the last 3 characters you can pass a number to #last.
要获取最后 3 个字符,您可以将一个数字传递给 #last。
"hello there".last(3) #=> "ere"
回答by Aram
In ruby you can use something like this:
在 ruby 中,您可以使用以下内容:
ending = str[-n..-1] || str
this return last n characters
此返回最后 n 个字符
回答by Breen ho
Slice()method will do for you.
Slice()方法将为您做。
For Ex
对于前
"hello".slice(-1)
# => "o"
Thanks
谢谢
回答by Pierre Ferry
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 ;.
您的代码有点有效,您看到的“奇怪数字”是;ASCII 代码。每个字符都有一个对应的 ascii 代码 ( https://www.asciitable.com/)。您可以用于对话puts last_char.chr,它应该输出;。

