Senthil Kumaran
Senthil Kumaran

Reputation: 56871

String / Locale ascii letters in Java

Coming from the Python background, in order to get all the alphabets, I am used to doing

>>> import string
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

There are similar helper constants for lower_case and upper_case. What is the equivalent in Java. I looked into String module and could not find any.

I had to manually create a char array and it seemed superflous (or perhaps non-idiomatic) to me.

Upvotes: 0

Views: 425

Answers (1)

Chamil
Chamil

Reputation: 803

There is no such specific method in java. Specially java is designed to work with Unicode which may support all the characters used over the world. So just having english alphabet as a constant doesn't mean anything.

public static String alphabet(char letter1, char letter2) { 
    StringBuffer out = new StringBuffer();    
    for (char c = letter1; c < letter2; c++) {
        out.append(c);
    }
    return out.toString();
}

you can write the above code and call it like

String output = alphabet('a','z') + alphabet('A','Z')

Upvotes: 1

Related Questions