Find a letter's alphabetical score [closed]

六眼飞鱼酱① 提交于 2019-12-24 16:37:24

问题


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

回答1:


"A".ord

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

which works like:

get_code('A') # 1
get_code('B') # 2
get_code('C') # 3


来源:https://stackoverflow.com/questions/33747126/find-a-letters-alphabetical-score

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!