Mic*_*ski 23
经过测试和工作
所有字母都可以用它们的ASCII值表示.
如果将字母转换为a int,添加1,然后转换回a char,则字母将增加1个ASCII值(下一个字母).
例如:
'a'是97
'b'的98
因此,如果输入是'a'并且您将其转换为a int,那么您将获得97.然后添加1并获取98,然后最终char再次将其转换为a 并获取'b'.
这是一个铸造的例子:
System.out.println( (int)('a') ); // 97
System.out.println( (int)('b') ); // 98
System.out.println( (char)(97) ); // a
System.out.println( (char)(98) ); // b
Run Code Online (Sandbox Code Playgroud)
所以,你的最终代码可能是这样的:
// get first char in the input string
char value = et.getText().toString().charAt(0);
int nextValue = (int)value + 1; // find the int value plus 1
char c = (char)nextValue; // convert that to back to a char
et.setText( String.valueOf(c) ); // print the char as a string
Run Code Online (Sandbox Code Playgroud)
当然,只有当一个字符作为输入时,这才能正常工作.
一种更简单的方法是:
char value = edt.getText().toString().charAt(0);
edt.setText(Character.toString ((char) value+1));
Run Code Online (Sandbox Code Playgroud)
在这里value + 1添加字符的十进制等效值并将其递增一..这是一个小图表:

什么发生在'z'之后?......它不会崩溃.. 请看这里的完整图表..
试试这个吧
String convertString = getIncrementStr("abc");
public static String getIncrementStr(String str){
StringBuilder sb = new StringBuilder();
for(char c:str.toCharArray()){
sb.append(++c);
}
return sb.toString();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23757 次 |
| 最近记录: |