Java Character isLetterOrDigit()与实例
java.lang.Character.isLetterOrDigit(char ch) 是java中的一个内置方法,用于确定指定字符是字母还是数字。
语法
public static boolean isLetterOrDigit(char ch)
参数: 该函数接受一个强制参数 ch ,表示要测试的字符。
返回值: 该函数返回一个布尔值。如果该字符是一个字母或数字,则布尔值为真,否则为假。
以下程序说明了上述方法。
程序1 :
// 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
程序2
// 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://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isLetterOrDigit(char)