字符串到Char ||字符串字符串

时间:2014-04-25 06:51:39

标签: java android

假设我有一个按钮

case R.id.button:

将执行以下功能:

int position;
    String keyInStringForm = et2.getText().toString();
            int keyInIntegerForm = Integer.parseInt(keyInStringForm);
            String text = et1.getText().toString();
    for (int i = 0; i < text.length(); i++) {
                    if (text.charAt(i) == 'a' || text.charAt(i) == 'A') {
                        position = 0;
                        break;
                    } else if (text.charAt(i) == 'b' || text.charAt(i) == 'B') {
                        position = 1;
                        break;
                    } else if (text.charAt(i) == 'c' || text.charAt(i) == 'C') {
                        position = 2;
                        break;
                    } else if (text.charAt(i) == 'd' || text.charAt(i) == 'D') {
                        position = 3;
                        break;
                    } else if (text.charAt(i) == 'e' || text.charAt(i) == 'E') {
                        position = 4;
                        break;
                    } else if (text.charAt(i) == 'f' || text.charAt(i) == 'F') {
                        position = 5;
                        break;
                    } else if (text.charAt(i) == 'g' || text.charAt(i) == 'G') {
                        position = 6;
                        break;
                    } else if (text.charAt(i) == 'h' || text.charAt(i) == 'H') {
                        position = 7;
                        break;
                    } else if (text.charAt(i) == 'i' || text.charAt(i) == 'I') {
                        position = 8;
                        break;
                    } else if (text.charAt(i) == 'j' || text.charAt(i) == 'J') {
                        position = 9;
                        break;
                    } else if (text.charAt(i) == 'k' || text.charAt(i) == 'K') {
                        position = 10;
                        break;
                    } else if (text.charAt(i) == 'l' || text.charAt(i) == 'L') {
                        position = 11;
                        break;
                    } else if (text.charAt(i) == 'm' || text.charAt(i) == 'M') {
                        position = 12;
                        break;
                    } else if (text.charAt(i) == 'n' || text.charAt(i) == 'N') {
                        position = 13;
                        break;
                    } else if (text.charAt(i) == 'o' || text.charAt(i) == 'O') {
                        position = 14;
                        break;
                    } else if (text.charAt(i) == 'p' || text.charAt(i) == 'P') {
                        position = 15;
                        break;
                    } else if (text.charAt(i) == 'q' || text.charAt(i) == 'Q') {
                        position = 16;
                        break;
                    } else if (text.charAt(i) == 'r' || text.charAt(i) == 'R') {
                        position = 17;
                        break;
                    } else if (text.charAt(i) == 's' || text.charAt(i) == 'S') {
                        position = 18;
                        break;
                    } else if (text.charAt(i) == 't' || text.charAt(i) == 'T') {
                        position = 19;
                        break;
                    } else if (text.charAt(i) == 'u' || text.charAt(i) == 'U') {
                        position = 20;
                        break;
                    } else if (text.charAt(i) == 'v' || text.charAt(i) == 'V') {
                        position = 21;
                        break;
                    } else if (text.charAt(i) == 'w' || text.charAt(i) == 'W') {
                        position = 22;
                        break;
                    } else if (text.charAt(i) == 'x' || text.charAt(i) == 'X') {
                        position = 23;
                        break;
                    } else if (text.charAt(i) == 'y' || text.charAt(i) == 'Y') {
                        position = 24;
                        break;
                    } else if (text.charAt(i) == 'z' || text.charAt(i) == 'Z') {
                        position = 25;
                        break;
                    } else if (text.charAt(i) == ' ') {
                        position = 26;
                        break;
                    }
                    int initialResult = position + keyInIntegerForm;
                    int finalResult = initialResult % 26;
                    char resultantChar = alphabets[finalResult];  

其中“alphabets”是a-z字符的char数组。

} // for

现在会有更多的那个“resulChar”,我希望将那些“resulChar”组合在一起形成一个字符串,这样我就可以将它设置为textview。 我该怎么做

3 个答案:

答案 0 :(得分:3)

请使用它来简化您的代码!

char ch = 'Z';
ch = Character.toLowerCase(ch);
int position = Character.getNumericValue(ch) - Character.getNumericValue('a');

或者,对于您的情况:

char ch = Character.toLowerCase(text.charAt(i));
if (ch >= 'a' && ch <= 'z') {
    position = Character.getNumericValue(ch) - Character.getNumericValue('a');
} else if (ch == ' ') {
    position = 26;
}

答案 1 :(得分:3)

如果我理解正确,请尝试执行以下操作:

StringBuffer result = new StringBuffer();

for (int i = 0; i < text.length(); i++) {
    ...

char resultantChar = alphabets[finalResult];

result.append(resultantChar);

}

System.out.println(result);

答案 2 :(得分:0)

使用http://developer.android.com/reference/java/lang/StringBuilder.html stringbuilder,你可以使用stringbuilder附加char