Java 将 char 值增加 1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22574768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Increase a char value by one
提问by Giannis Thanasiou
I have an app with an EditText
and a button named "forward". All I want is just when I type "a" in the EditText
and click the button, the word "b" to be typed in the EditText
, "c" if pressed again and so on. I have tried:
我有一个带有一个EditText
名为“转发”的按钮的应用程序。我想要的只是当我在中键入“a”EditText
并单击按钮时,在“b”中键入单词EditText
“c”,如果再次按下,依此类推。我试过了:
value = edt.getText();
edt.setText(value + 1);
But that of course will print the initial string followed by the number "1". Any ideas? Thanks a lot
但这当然会打印初始字符串,后跟数字“1”。有任何想法吗?非常感谢
采纳答案by Michael Yaworski
Tested and works
测试和工作
All letters can be represented by their ASCIIvalues.
所有字母都可以用它们的ASCII值来表示。
If you cast the letters to an int
, add 1, and then cast back to a char
, the letter will increase by 1 ASCII value (the next letter).
如果将字母转换为 an int
,加 1,然后转换回 a char
,则该字母将增加 1 个 ASCII 值(下一个字母)。
For example:
例如:
'a'
is 97
'b'
is 98
'a'
是97
'b'
是98
So if the input was 'a'
and you casted that to an int
, you would get 97
. Then add 1 and get 98
, and then finally cast it back to a char
again and get 'b'
.
因此,如果输入是'a'
并且您将其强制转换为int
,您将得到97
. 然后加 1 和 get 98
,然后最后将它char
再次转换回 a并 get 'b'
。
Here is an example of casting:
下面是一个铸造的例子:
System.out.println( (int)('a') ); // 97
System.out.println( (int)('b') ); // 98
System.out.println( (char)(97) ); // a
System.out.println( (char)(98) ); // b
So, your final code might be this:
所以,你的最终代码可能是这样的:
// 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
Of course this will only work properly if there is one single character as an input.
当然,这只有在输入一个字符时才能正常工作。
回答by Lefteris
You need to convert the text value (character) to the ascii value, then increase it, not just the text.
您需要将文本值(字符)转换为 ascii 值,然后增加它,而不仅仅是文本。
value = edt.getText().toString();
int ascii = (int)value;
edt.setText(Character.toString ((char) ascii+1));
回答by amalBit
A simpler way would be:
一个更简单的方法是:
char value = edt.getText().toString().charAt(0);
edt.setText(Character.toString ((char) value+1));
Here the value + 1
adds the decimal equivalent of the character and increments it by one.. Here is a small chart:
这里value + 1
添加了字符的十进制等效值并将其增加一.. 这是一个小图表:
Whats happens after 'z'? ... it wont crash.. see here for the full chart..
'z' 之后会发生什么?...它不会崩溃..查看完整图表。.
回答by Philip Liberato
String value = edt.getText();
edt.setText(value + 1);
If the above is your code, then what you are doing is concatenating a 1 to the end of your value
string. Thus, a button click would alter the displayed text from "your text" to "your text1". This process would continue on the next button click to show "your text11". The issue is really a type error.
如果以上是您的代码,那么您所做的就是将 1 连接到value
字符串的末尾。因此,单击按钮会将显示的文本从“您的文本”更改为“您的文本 1”。此过程将在下一次单击按钮时继续以显示“您的 text11”。问题确实是类型错误。
回答by mok
Try this:
尝试这个:
edt.setText(Character.toString((char)(edt.getText().charAt(0) + 1)));
回答by Aravin
Try this one
试试这个
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();
}