问题
I'm quite new to Java so I am wondering how do you convert a letter in a string to a number e.g. hello world would output as 8 5 12 12 15 23 15 18 12 4.
so a=1, b=2, z=26 etc.
回答1:
Since this is most likely a learning assignment, I'll give you a hint: all UNICODE code points for the letters of the Latin alphabet are ordered alphabetically. If the code of a is some number N, then the code of b is N+1, the code of c is N+2, and so on; the code of Z is N+26.
You can subtract character code points in the same way that you subtract integers. Since the code points are alphabetized, the following calculation
char ch = 'h';
int pos = ch - 'a' + 1;
produces the sequence number of h, i.e. 8. If you perform this calculation in a loop, you would get the result that you need.
Note that the above formula works only with characters of the same register. If your input string is in mixed case, you need to convert each character to lower case before doing the calculation, otherwise it would come out wrong.
回答2:
String s = "hello world";
String t = "";
for (int i = 0; i < s.length(); ++i) {
char ch = s.charAt(i);
if (!t.isEmpty()) {
t += " ";
}
int n = (int)ch - (int)'a' + 1;
t += String.valueOf(n);
}
System.out.println(t);
This does not deal with space etc.
回答3:
public static void main(String[] args) {
String s = "hello world";
s = s.replace(" ", "");
char[] c = s.toCharArray();
for (Character ss : c)
System.out.println(ss - 'a' + 1);
}
回答4:
Usa a Map with the key being the character and an value being the integers. This is not an efficient way - the map should be a static member of the class.
import java.util.HashMap;
import java.util.Map;
public class JavaApplication1
{
public static void main(String[] args)
{
final Map<Character, Integer> map;
final String str = "hello world";
map = new HashMap<>();
// or map = new HashMap<Character, Integer> if you are using something before Java 7.
map.put('a', 1);
map.put('b', 2);
map.put('c', 3);
map.put('d', 4);
map.put('e', 5);
map.put('f', 6);
map.put('g', 7);
map.put('h', 8);
map.put('i', 9);
map.put('j', 10);
map.put('k', 11);
map.put('l', 12);
map.put('m', 13);
map.put('n', 14);
map.put('o', 15);
map.put('p', 16);
map.put('q', 17);
map.put('r', 18);
map.put('s', 19);
map.put('t', 20);
map.put('u', 21);
map.put('v', 22);
map.put('w', 23);
map.put('x', 24);
map.put('y', 25);
map.put('z', 26);
for(final char c : str.toCharArray())
{
final Integer val;
val = map.get(c);
if(val == null)
{
// some sort of error
}
else
{
System.out.print(val + " ");
}
}
System.out.println();
}
}
回答5:
for each character at posotion i: output s.charAt(i)-'a'+1. s is the string.
回答6:
You can do something like:
for (int i = 0; i < a.length(); ++i) {
if (a.charAt(i) >= 'a' && a.charAt(i) <= 'z') {
System.out.println((int)a.charAt(i) - (int)'a');
}
}
回答7:
If you need you can use below tested code to convert string into number if your string contains only numbers and alphabets.
public static Long getNumericReferenceNumber(String str) {
String result = "";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (Character.isLetter(ch)) {
char initialCharacter = Character.isUpperCase(ch) ? 'A' : 'a';
result = result.concat(String.valueOf((ch - initialCharacter + 1)));
} else result = result + ch;
}
return Long.parseLong(result);
}
回答8:
I have added all the characters to get a fair result:
public static long stringToNumber(String s) {
long result = 0;
for (int i = 0; i < s.length(); i++) {
final char ch = s.charAt(i);
result += (int) ch;
}
return result;
}
来源:https://stackoverflow.com/questions/15027231/java-how-to-convert-letters-in-a-string-to-a-number