Strings to Integers

前端 未结 4 343
温柔的废话
温柔的废话 2021-01-26 09:57

Say you have the string \"Hi\". How do you get a value of 8, 9 (\"H\" is the 8th letter of the alphabet, and \"i\"

4条回答
  •  旧时难觅i
    2021-01-26 10:32

    Preserving case and assuming you want to wrap around at "Z":

    upper = [*?A..?Z]
    lower = [*?a..?z]
    LOOKUP = (upper.zip(upper.rotate) + lower.zip(lower.rotate)).to_h
    s.each_char.map { |c| LOOKUP[c] }.join
    #=> "Ij"
    

提交回复
热议问题