Java - Character isLetter() method



The Java Character isLetter() method determines if the specified character is a letter.

A character is considered to be a letter if its general category type, the return value obtained by the Character.getType() method, is any of the following −

  • UPPERCASE_LETTER

  • LOWERCASE_LETTER

  • TITLECASE_LETTER

  • MODIFIER_LETTER

  • OTHER_LETTER

Not all letters have case; many characters are letters but are neither uppercase nor lowercase nor titlecase.

Syntax

Following is the syntax for Java Character isLetter() method

public static boolean isLetter(char ch)
(or)
public static boolean isLetter(int codePoint)

Parameters

  • ch − the character to be tested

  • codePoint − the Unicode code point to be tested

Return Value

This method returns true if the character argument is a letter, otherwise false.

Example

The following example shows the usage of Java Character isLetter(char ch) method.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 primitives ch1 (char), cp1 (int)
      char ch1;
      int cp1;

      // assign values to ch1, cp1
      ch1 = 'A';
      cp1 = 0x0045;

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      // check if ch1, cp1 are letter or not and assign results to b1, b2
      b1 = Character.isLetter(ch1);
      b2 = Character.isLetter(cp1);
      String str1 = ch1 + " is a letter is " + b1;
      String str2 = (char)cp1 + " is a letter is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

A is a letter is true
E is a letter is true

Example

The following example shows the usage of Java Character isLetter(int codePoint) method.

import java.lang.*;
public class isLetterDemo{
   public static void main(String []args){
      int cp = 0x0012;
      boolean b;        
      b = Character.isLetter(cp);        
      System.out.println(b);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

false

Example

Another example to check whether the input character is a letter or not using the method is given below

import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 primitives ch1 (char), cp1 (int) and assign titlecase letters
      Character ch1 = new Character('ĵ');
	   int cp1 = 0x01C5;

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      // check if ch1, cp1 are letter or not and assign results to b1, b2
      b1 = Character.isLetter(ch1);
      b2 = Character.isLetter(cp1);

      // print b1, b2 values
      System.out.println(ch1 + " is a letter is " + b1);
      System.out.println("cp1 character is a letter: " + b2);
   }
}

Output

If we compile and run the program, we get the following output −

ĵ is a letter is true
cp1 character is a letter: true

Example

Let us now look at an example that determines whether the modifier letters are letters using this method.

import java.lang.*;
public class Main {
   public static void main(String[] args) {

      // create 2 primitives ch1 (char), cp1 (int)
      Character ch1 = new Character('ʲ');
	   int cp1 = 0x02C0;

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      // check if ch1, cp1 are letter or not and assign results to b1, b2
      b1 = Character.isLetter(ch1);
      b2 = Character.isLetter(cp1);

      // print b1, b2 values
      System.out.println(ch1 + " is a letter is " + b1);
      System.out.println("cp1 character is a letter: " + b2);
   }
}

Output

The output is displayed after compiling and running the above program as follows −

ʲ is a letter is true
cp1 character is a letter: true
java_lang_character.htm
Advertisements