isLetter
method belongs to java.lang.Character
class. It is a static
method which takes a character as argument and tells if it is a letter or not.
A letter here means a character between A - Z
or a - z
. Digits and special characters are not considered as letters.
This method returns true
if the supplied character is a letter and false
if it is not a letter.
There are two overloaded versions of
isLetter
method in java.lang.Character
class.Both of these are explained below.
1. isLetter(char c) : This method takes a character as argument and returns
true
if the supplied character is a letter(lies between A-Z and a-z). Returns false
if the supplied character is not among these. 2. isLetter(int c): This method takes an integer as argument and returns true
if the supplied integer value corresponds to a character in the range A - Z
and a - z
, that is between 65 – 90 and 97 – 122.
Remember that the integers represent the ASCII values of characters.
Using isLetter
As stated above, isLetter
is a static
method in java.lang.Character
class.
Thus, it can be invoked directly by using the class name as shown below.
Character.isLetter(argument)
Here, the argument can be a char
or an int
as explained in the previous section.
isLetter Example
Following are the examples of isLetter
method which accepts a character with different arguments.
public class IsLetterDemo { public static void main(String[] args) { //check for an alphabet char character = 'f'; boolean isletter = Character.isLetter(character); System.out.println(character + " : " + isletter); // check for a digit character = '9'; isletter = Character.isLetter(character); System.out.println(character + " : " + isletter); // check for a special character character = ','; isletter = Character.isLetter(character); System.out.println(character + " : " + isletter); } }
Output of above example is
f : true
9 : false
, : false
isLetter
method which accepts an integer with different arguments.Second one is a value that is a valid letter or alphabet(
C
), other values do not correspond to a letter and the output below confirms that. public class IsLetterDemo { public static void main(String[] args) { int character = 12; boolean isletter = Character.isLetter(character); System.out.println(character + " : " + isletter); character = 67; isletter = Character.isLetter(character); System.out.println(character + " : " + isletter); character = 123; isletter = Character.isLetter(character); System.out.println(character + " : " + isletter); } }
Output of above code is
12 : false
67 : true
123 : false
Use of isLetter
isLetter
is a convenience method to check if a character is an alphabetic letter.
It can be used to check the presence of a number or a special character inside a string by iterating over all the characters and checking if it contains a character other than alphabets.