Java - Character isLetterOrDigit() Method



The Java Character isLetterOrDigit() method determines if the specified character is a letter or digit.

Here, a character is considered to be a letter or digit if either Character.isLetter() method or Character.isDigit() method returns true for the said character.

Note − This method occurs in two polymorphic forms: with different parameters but same return type. The polymorphic method with character argument does not handle supplementary characters so we use another polymorphic form of this method that takes code points as arguments.

Syntax

Following is the syntax for Java Character isLetterOrDigit() method

public static boolean isLetterOrDigit(char ch)
(or)
public static boolean isLetterOrDigit(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 is a letter or digit, false otherwise.

Example

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

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

      // create 2 char primitives ch1, ch2
      char ch1, ch2;

      // assign values to ch1, ch2
      ch1 = 'A';
      ch2 = '1';

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

      /**
       *  check if ch1, ch2 are letter or digit and assign
       *  results to b1, b2
       */
      b1 = Character.isLetterOrDigit(ch1);
      b2 = Character.isLetterOrDigit(ch2);
      String str1 = ch1 + " is a letter/digit is " + b1;
      String str2 = ch2 + " is a letter/digit 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/digit is true
1 is a letter/digit is true

Example

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

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

      // create 2 int primitives cp1, cp2
      int cp1, cp2;

      // assign values to cp1, cp2
      cp1 = 0x0033;
      cp2 = 0x012b23;

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

      /**
       *  check if cp1, cp2 represents letter/digit and
       *  assign results to b1, b2
       */
      b1 = Character.isLetterOrDigit(cp1);
      b2 = Character.isLetterOrDigit(cp2);

      String str1 = "cp1 represents a letter/digit is " + b1;
      String str2 = "cp2 represents a letter/digit 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 −

cp1 represents a letter/digit is true
cp2 represents a letter/digit is false

Example

Another example to understand the method with symbol arguments passed is shown below –

import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      char c1;
      c1 = '&';
      boolean b;
      b = Character.isLetterOrDigit(c1);
      System.out.println(c1 + " represents a letter/digit is " + b);
   }
}

Output

Output is obtained and displayed as −

& represents a letter/digit is false

Example

This method can also be called in loop to check whether an array of char values are either a letter or a digit.

In the following example, we declare and initialize a char array with length 6. Using a for loop, we call the method by passing each element of the array as its argument. The return values are also obtained in this loop statement itself.

import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      char c1[] = {'1', 'a', '%', 'f', '9', '#'};
      for(int i = 0; i < c1.length; i++) {
         boolean b;
         b = Character.isLetterOrDigit(c1[i]);
         System.out.println(c1[i] + " represents a letter/digit is " + b);
      }
   }
}

Output

Upon compiling and executing the given program above, the output will be displayed as −

1 represents a letter/digit is true
a represents a letter/digit is true
% represents a letter/digit is false
f represents a letter/digit is true
9 represents a letter/digit is true
# represents a letter/digit is false
java_lang_character.htm
Advertisements