[英]I need advice with string.index
I'd like to get this line of ruby code working(I always get 0). 我想让这行红宝石代码正常工作(我总是得到0)。
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ."
letter = gets
puts alphabet.index(letter).to_i
I'd like to get the position of the letter in the alphabet. 我想知道字母在字母表中的位置。
Try following, 尝试跟随,
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ."
letter = gets.chomp
puts alphabet.index(letter)
You need to chomp \\n
coming after letter entered which is the result of pressing "enter" afterwards, 您需要在输入字母后紧跟
\\n
,这是随后按“ Enter”的结果,
> letter = gets
B
=> "B\n"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.