"Auto increment" alphabet in Java - is this possible? From A to Z without a third-party library?
In Java, the char variable stores the ASCII value of a character (number between 0 and 127) rather than the character itself. The ASCII value of lowercase alphabets are from 97 to 122. And, the ASCII value of uppercase alphabets are from 65 to 90. That is, alphabet a is stored as 97 and alphabet z is stored as 122.
Basically it increments letters like the column ID's of an Excel spreadsheet. nextChar('yz'); // returns "ZA" function nextChar(c) { var u = c. toUpperCase(); if (same(u,'Z')){ var txt = ''; var i = u. length; while (i--) { txt += 'A'; } return (txt+'A'); } else { var p = ""; var q = ""; if(u.
You can't. Strings are immutable.
Yes, you can do it like this:
for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
System.out.println(alphabet);
}
It is also possible with typecasting:
for (int i = 65; i <= 90; i++) {
System.out.println((char)i);
}
Yes, like this:
for (int i = 0; i < 26; i++)
{
char upper = (char) ('A' + i);
char lower = (char) ('a' + i);
...
}
for (char c = 'A'; c <= 'Z'; c++) {
...
}
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