Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alphabet constant in Java?

Tags:

java

constants

I have a situation where I need to find a letter's index in the alphabet. In Python I could use string.ascii_lowercase or string.ascii_uppercase. Is there something similar in Java?

Obviously I could do:

private static char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();

But after so much Python, it makes me wonder if this is built in somewhere.

like image 574
Brendan Long Avatar asked Jun 01 '11 18:06

Brendan Long


1 Answers

You can get the index like this:

char lowercaseLetter = ...
int index = lowercaseLetter - 'a';
like image 68
ColinD Avatar answered Nov 09 '22 08:11

ColinD