📜  Java中的字符.isLetterOrDigit() 和示例

📅  最后修改于: 2022-05-13 01:55:11.522000             🧑  作者: Mango

Java中的字符.isLetterOrDigit() 和示例

Java.lang。 字符.isLetterOrDigit(char ch)是Java中的一个内置方法,用于确定指定字符是字母还是数字。

句法:

public static boolean isLetterOrDigit(char ch)

参数:该函数接受一个强制参数ch ,它表示要测试的字符。

返回值:此函数返回一个布尔值。如果字符是字母或数字,则布尔值为true ,否则为false

下面的程序说明了上述方法:

方案一:

// Java program to illustrate the
// Character.isLetterOrDigit() method
  
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // two characters
        char c1 = 'Z', c2 = '2';
  
        // Function to check if the character is letter or digit
        boolean bool1 = Character.isLetterOrDigit(c1);
        System.out.println(c1 + " is a letter/digit ? " + bool1);
  
        // Function to check if the character is letter or digit
        boolean bool2 = Character.isLetterOrDigit(c2);
        System.out.println(c2 + " is a letter/digit ? " + bool2);
    }
}
输出:
Z is a letter/digit ? true
2 is a letter/digit ? true

方案二:

// Java program to illustrate the
// Character.isLetterOrDigit() method
  
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // assign character
        char c1 = 'D', c2 = '/';
  
        // Function to check if the character is letter or digit
        boolean bool1 = Character.isLetterOrDigit(c1);
        System.out.println(c1 + " is a letter/digit ? " + bool1);
  
        // Function to check if the character is letter or digit
        boolean bool2 = Character.isLetterOrDigit(c2);
        System.out.println(c2 + " is a letter/digit ? " + bool2);
    }
}
输出:
D is a letter/digit ? true
/ is a letter/digit ? false

参考: https: Java/lang/ 字符.html#isLetterOrDigit(char)