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?
相关问题
- Do the Java Integer and Double objects have unnece
- how to split a list into a given number of sub-lis
- Generate string from integer with arbitrary base i
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
相关文章
- JSP String formatting Truncate
- Ruby using wrong version of openssl
- Difference between Thread#run and Thread#wakeup?
- Selecting only the first few characters in a strin
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- Segmentation fault with ruby 2.0.0p247 leading to
- How to detect if an element exists in Watir
You can also create an enumerable of character ordinals from a string using the
codepoints
method.Note Cary Swoveland had already given a same answer in a comment to the question.
It is impossible to do that through the numbers 8 and 9 because these numbers do not contain information about the case of the letters. But if you do not insist on converting the string via the number 8 and 9, but instead more meaningful numbers like ASCII code, then you can do it like this:
Preserving case and assuming you want to wrap around at "Z":
use
ord
to get the ASCII index, andchr
to bring it back.