Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'.

like image 446
raleighJ Avatar asked May 24 '10 18:05

raleighJ


People also ask

What is a method that can be used to increment letters?

You can try this console. log( 'a'. charCodeAt​(0))​ First convert it to Ascii number .. Increment it .. then convert from Ascii to char..

How do you increment a character in PHP?

PHP has a built-in way of incrementing either a number or a string simply by placing ++ at the end of the variable. echo $i; But you can also do this for letters; PHP will increment the variable to the next letter in the alphabet.

How do you get the next letter in JavaScript?

To get the next letter of the alphabet in JavaScript, we can use the String. fromCharCode static string method and the charCodeAt string instance method.


1 Answers

One way:

String value = "C";
int charValue = value.charAt(0);
String next = String.valueOf( (char) (charValue + 1));
System.out.println(next);
like image 83
Kevin Avatar answered Oct 10 '22 16:10

Kevin