查找字母的字母分数 [英] Find a letter's alphabetical score
本文介绍了查找字母的字母分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在字母中查找字符位置的正确方法是什么?例如:
What would be the right way to find the location of a character within the alphabet? For example:
"A".find_score # => 1
"C".find_score # => 3
推荐答案
"A".ord
返回65,即 A的数字代码,即字母的开头在。如果希望它从1开始,则可以减去64:
returns 65, the numeric code for "A", which is what the alphabet starts at. If you want it to start at 1 you could just subtract 64:
def get_code(c)
c.upcase.ord - 'A'.ord + 1
end
的工作原理是:
get_code('A') # 1
get_code('B') # 2
get_code('C') # 3
这篇关于查找字母的字母分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文