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'
.
You can try this console. log( 'a'. charCodeAt(0)) First convert it to Ascii number .. Increment it .. then convert from Ascii to char..
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.
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.
One way:
String value = "C";
int charValue = value.charAt(0);
String next = String.valueOf( (char) (charValue + 1));
System.out.println(next);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With