Strings to Integers

前端 未结 4 371
温柔的废话
温柔的废话 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条回答
  •  攒了一身酷
    2021-01-26 10:17

    use ord to get the ASCII index, and chr to bring it back.

    'Hi'.chars.map{|x| (x.ord+1).chr}.join
    

提交回复
热议问题