字符串整数 [英] Strings to Integers
本文介绍了字符串整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设你有字符串嗨
。如何获得 8
的值, 9
(H
是字母表的第8个字母,i
是第9个字母。然后说,将 1
添加到这些整数并使其 9
, 10
然后可以回到字符串Ij
?有可能吗?
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"
is the 9th letter). Then say, add 1
to those integers and make it 9
, 10
which can then be made back into the string "Ij"
? Is it possible?
推荐答案
使用 ord
来获取ASCII索引,和 chr
将它带回来。
use ord
to get the ASCII index, and chr
to bring it back.
'Hi'.chars.map{|x| (x.ord+1).chr}.join
这篇关于字符串整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文