我想以我的方式获取字符串中的最后一个字符——1)获取最后一个索引2)获取最后一个索引中的字符,作为字符串.之后,我将把字符串与另一个字符串进行比较,但我不会在这里包含这部分代码.我try 了下面的代码,结果得到了一个奇怪的数字.我使用的是ruby 1.8.7.
为什么会这样,我该怎么做?
line = "abc;"
last_index = line.length-1
puts "last index = #{last_index}"
last_char = line[last_index]
puts last_char
输出-
last index = 3
59
Ruby docs告诉我数组切片是这样工作的-
a = "hello there"
a[1] #=> "e"
但是,在我的代码中,它不是.