我已将“a 到 z”整数及其各自的整数/数字值硬编码。 喜欢
int a=0, b=1, c=2, d=3, e=4, f=5, g=6;
现在我有了一个编辑文本,它可以接受像 abcdefg 这样的输入。我想将字符串的每个字符转换为 int 这样我就可以得到 0123456 返回 abcdefg 那可能吗?请帮忙。提前致谢。
最佳答案
为此最好使用 hashmap
删除散列映射中的值,如下所示
HashMap<String,Integer> map = new HashMap<String,Integer>();
map.put("a", 0);
map.put("b", 1);
map.put("c", 2);
map.put("d", 3);
//....
map.put("z",25);
String s1 ="abcdefg";//String from edit text
char[] sa = s1.toCharArray();//converting to character array.
String str ="";
for(int i=0;i<sa.length;i++)
{
str = str+(map.get(Character.toString(sa[i])));
}
System.out.println(str);//Here str show the exact result what you required.
关于android - 将字符串的每个字符转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12526810/