android - 将字符串的每个字符转换为整数

标签 android string

我已将“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/

相关文章:

c++ - C++ 字符串类可以进行指针运算吗?

c++ - 一次性对字符串数组进行排序

android - Android中如何知道EditText发生了删除操作?

android - v4.app.NotificationCompat 和 v7.app.NotificationCompat 有什么区别?

c++ - 比较 50 个以上字符串的有效方法

文本区域中的 JavaScript 字符串替换

python - 为什么当我使用 beautifulsoup 抓取 HTML 中的字符串时它们不起作用

Android 测试 BuildConfig 字段

java - 从代码更改铃声

android - ExpandableListView 扩展组