What is the best and/or easiest way to recognize if a string.charAt(index) is an A-z letter or a number in Java without using regular expressions? Thanks.
9 Answers
Character.isDigit(string.charAt(index))
(JavaDoc) will return true if it's a digit
Character.isLetter(string.charAt(index))
(JavaDoc) will return true if it's a letter

- 43,763
- 16
- 104
- 144
-
17Note: that these tell you if the character is a Unicode letter / digit. The OP asked for "an A-z letter" ... whatever that means. – Stephen C Oct 29 '10 at 00:58
-
5Why does the ASCII ├ (255 ) pass in my case? I thought its for a-z, A-Z and 0-9 only? – mr5 Aug 27 '15 at 06:04
-
@CᴏɴᴏʀO'Bʀɪᴇɴ Links are now fixed. Thanks for letting me know. – Adam Feb 06 '17 at 23:06
-
20Use`Character.isLetterOrDigit(string.charAt(index))` for both the verifications. – Aspirant9 Feb 16 '17 at 03:12
-
3Be careful, isLetterOrDigit gives true on way more than a-Z0-9 !!! refer to the doc here https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isLetterOrDigit(char) – fl0w Mar 06 '19 at 16:49
I'm looking for a function that checks only if it's one of the Latin letters or a decimal number. Since char c = 255
, which in printable version is ├ and considered as a letter by Character.isLetter(c)
.
This function I think is what most developers are looking for:
private static boolean isLetterOrDigit(char c) {
return (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9');
}

- 3,438
- 3
- 40
- 57
-
1Just went through our code and was amazed how many bugs were in there because of isLetter and isLetterOrDigit... Thank you! – fl0w Mar 06 '19 at 16:47
-
2Somehow you have gotten your character-sets and or display fonts mixed up. Unicode codepoint `u00ff` is actually the character ÿ. (Lower-case y with an umlaut.) The codepoint that represents ├ is `u251c`. – Stephen C Jun 01 '20 at 11:56
-
@StephenC you're right. I forgot how I end up typing that character instead of [nbsp](https://theasciicode.com.ar/extended-ascii-code/non-breaking-space-no-break-space-ascii-code-255.html) – mr5 Jun 03 '20 at 15:46
-
2On Kotlin it is much simpler `if (c in 'a'..'z' || с in 'A'..'Z' || c in '0'..'9')` – Vlad Jul 14 '20 at 07:58
As the answers indicate (if you examine them carefully!), your question is ambiguous. What do you mean by "an A-z letter" or a digit?
If you want to know if a character is a Unicode letter or digit, then use the
Character.isLetter
andCharacter.isDigit
methods.If you want to know if a character is an ASCII letter or digit, then the best thing to do is to test by comparing with the character ranges 'a' to 'z', 'A' to 'Z' and '0' to '9'.
Note that all ASCII letters / digits are Unicode letters / digits ... but there are many Unicode letters / digits characters that are not ASCII. For example, accented letters, cyrillic, sanskrit, ...
The general solution is to do this:
Character.UnicodeBlock block = Character.UnicodeBlock.of(someCodePoint);
and then test to see if the block is one of the ones that you are interested in. In some cases you will need to test for multiple blocks. For example, there are (at least) 4 code blocks for Cyrillic characters and 7 for Latin. The Character.UnicodeBlock
class defines static constants for well-known blocks; see the javadocs.
Note that any code point will be in at most one block.

- 698,415
- 94
- 811
- 1,216
Java Character class has an isLetterOrDigit method since version 1.0.2

- 1,528
- 1
- 16
- 22
-
1Be careful, isLetterOrDigit gives true on way more than a-Z0-9 !!! refer to the doc here https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isLetterOrDigit(char) – fl0w Mar 06 '19 at 16:46
I don't know about best, but this seems pretty simple to me:
Character.isDigit(str.charAt(index))
Character.isLetter(str.charAt(index))

- 96,106
- 25
- 196
- 225
-
Be careful, isLetterOrDigit gives true on way more than a-Z0-9 !!! refer to the doc here https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isLetterOrDigit(char) – fl0w Mar 06 '19 at 16:48
// check if ch is a letter
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
// ...
// check if ch is a digit
if (ch >= '0' && ch <= '9')
// ...
// check if ch is a whitespace
if ((ch == ' ') || (ch =='\n') || (ch == '\t'))
// ...
Source: https://docs.oracle.com/javase/tutorial/i18n/text/charintro.html

- 447
- 6
- 14
-
1The preceding code is wrong because it works only with English and a few other languages. To internationalize the previous example, replace it with the following statements: char ch; // ... // This code is OK! if (Character.isLetter(ch)) // ... if (Character.isDigit(ch)) // ... if (Character.isSpaceChar(ch)) // ... – Yao Li Jul 20 '17 at 06:10
-
OP clearly asked `if a string.charAt(index) is an A-z letter`. So we are not talking about other languages are we ? – vadasambar Jul 20 '17 at 08:57
-
Use the below code
Character.isLetterOrDigit(string.charAt(index))

- 137
- 1
- 3
-
2What does your answer add that hasn't been covered in the previous answers? – Robert Aug 27 '18 at 23:18
-
1Be careful, isLetterOrDigit gives true on way more than a-Z0-9 !!! refer to the doc here https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isLetterOrDigit(char) – fl0w Mar 06 '19 at 16:49
-
Robert, instead of calling two functions you can of course just call one function. – sheikh May 08 '19 at 09:16
Compare its value. It should be between the value of 'a' and 'z', 'A' and 'Z', '0' and '9'

- 8,672
- 7
- 44
- 65
-
1This manual approach is better than the built-in `Character.isLetter()` method? – IgorGanapolsky Dec 16 '15 at 19:10
-
1@IgorGanapolsky - It depends precisely what you are trying to do. Hint: they do different things! – Stephen C Jul 26 '16 at 07:53
-
@StephenC I thought `Character.isLetter()` is rudimentary. Unless we are talking about internationalization? – IgorGanapolsky Jul 26 '16 at 12:31
-
1@IgorGanapolsky - Read the javadocs. Then check the Unicode specs for what code-points the respective character classes actually contain. >>Of course<< we are talking about internationalization. Characters in Java are all Unicode based. – Stephen C Jul 26 '16 at 13:44
-
import java.util.Scanner;
public class v{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
String str;
int l;
int flag=0;
System.out.println("Enter the String:");
str=in.nextLine();
str=str.toLowerCase();
str=str.replaceAll("\\s","");
char[] ch=str.toCharArray();
l=str.length();
for(int i=0;i<l;i++){
if ((ch[i] >= 'a' && ch[i]<= 'z') || (ch[i] >= 'A' && ch[i] <= 'Z')){
flag=0;
}
else
flag++;
break;
}
if(flag==0)
System.out.println("Onlt char");
}
}

- 21
- 2