Java 如何将变量增加到字母表中的下一个或上一个字母?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2899301/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:17:51  来源:igfitidea点击:

How do I increment a variable to the next or previous letter in the alphabet?

javastringvariablesalphabet

提问by raleighJ

I have a capital letter defined in a variable string, and I want to output the next and previous letters in the alphabet. For example, if the variable was equal to 'C', I would want to output 'B'and 'D'.

我在变量字符串中定义了一个大写字母,我想输出字母表中的下一个和上一个字母。例如,如果变量等于'C',我想输出'B''D'

采纳答案by Kevin

One way:

单程:

String value = "C";
int charValue = value.charAt(0);
String next = String.valueOf( (char) (charValue + 1));
System.out.println(next);

回答by Bozho

If you are limited to the latin alphabet, you can use the fact that the characters in the ASCII table are ordered alphabetically, so:

如果您仅限于拉丁字母表,则可以使用 ASCII 表中的字符按字母顺序排列的事实,因此:

System.out.println((char) ('C' + 1));
System.out.println((char) ('C' - 1));

outputs Dand B.

输出DB

What you do is add a charand an int, thus effectively adding the intto the ascii code of the char. When you cast back to char, the ascii code is converted to a character.

你要做的是添加一个char和一个int,从而有效地将int加到char. 当您char转换回 时,ascii 代码将转换为字符。

回答by TacB0sS

Well if you mean the 'ABC' then they split into two sequences a-z and A-Z, the simplest way I think would be to use a char variable and to increment the index by one.

好吧,如果您的意思是“ABC”,那么它们会分成两个序列 az 和 AZ,我认为最简单的方法是使用 char 变量并将索引增加 1。

char letter='c';
letter++;  // (letter=='d')

same goes for decrement:

递减也是如此:

char letter='c';
letter--; // (letter=='b')

thing is that the representation of the letters a-z are 97-122 and A-Z are 65-90, so if the case of the letter is important you need to pay attention to it.

事情是字母 az 的表示是 97-122 和 AZ 是 65-90,所以如果字母的大小写很重要,你需要注意它。

回答by Daniel

just like this :

像这样 :

System.out.printf("%c\n",letter);
letter++;

回答by Sir Codesalot

All the answers are correct but none seem to give a full explanation so I'll try. Just like any other type, a charis stored as a number (16-bit in Java). Unlike other non-numeric types, the mapping of the values of the stored numbers to the values of the chars they represent are well known. This mapping is called the ASCII Table. The Java compiler treats chars as a 16-bit number and therefore you can do the following:

所有的答案都是正确的,但似乎没有一个给出完整的解释,所以我会尝试。就像任何其他类型一样,achar存储为数字(Java 中的 16 位)。与其他非数字类型不同,存储数字的值到char它们所代表的s值的映射是众所周知的。此映射称为ASCII 表。Java 编译器将chars 视为 16 位数字,因此您可以执行以下操作:

System.out.print((int)'A'); // prints 65
System.out.print((char)65); // prints A

For this reason, the ++, --and other mathematical operations apply to chars and provide a way to increment\decrement their values.

出于这个原因,++,--和其他数学运算适用于chars 并提供了一种递增\递减它们的值的方法。

Note that the casting is cyclic when you exceed 16-bit:

请注意,当您超过 16 位时,转换是循环的:

System.out.print((char)65601); // also prints A
System.out.print((char)-65471); // also prints A

P.S.This also applies to Kotlin:

PS这也适用于 Kotlin:

println('A'.toInt()) // prints 65
println(65.toChar()) // prints A
println(65601.toChar()) // prints A
println((-65471).toChar()) // prints A