字符类的 isLetterOrDigit(char ch) 方法确定给定(或指定)字符是字母还是数字。
如果给定字符的 Character.isLetter(char ch) 或 Character.isDigit(char ch) 方法为真,则该字符被视为字母或数字。
注意:上述方法不能用于处理增补字符。为了处理增补字符,我们可以使用 isLetterOrDigit(int) 方法。
用法
public static boolean isLetterOrDigit(char ch)
参数
上述方法只需要一个参数:
a.) 需要测试的字符。
返回值
如果给定(或指定)字符是字母或数字,则 isLetterOrDigit(char ch) 方法返回一个布尔值,即 true。否则,该方法返回 false。
例子1
public class JavaCharacterisLetterOrDigitExample1 {
public static void main(String[] args) {
// Create four char primitives ch1, ch2, ch3 and ch4.
char ch1;
char ch2;
char ch3;
char ch4;
// Assign the values to ch1, ch2, ch3 and ch4.
ch1 = 'A';
ch2 = '1';
ch3 = 'U';
ch4 = '2';
// Create four boolean primitives b1, b2, b3 and b4.
boolean b1 = Character.isLetterOrDigit(ch1);
boolean b2 = Character.isLetterOrDigit(ch2);
boolean b3 = Character.isLetterOrDigit(ch3);
boolean b4 = Character.isLetterOrDigit(ch4);
String str1 = "The first character '"+ ch1+ "' is a letter or digit:" + b1;
String str2 = "The second character '"+ch2+"' is a letter or digit:" + b2;
String str3 = "The third character '"+ch3+ "' is a letter or digit:" + b3;
String str4 = "The fourth character '"+ch4+ "' is a letter or digit:" + b4;
// Print the values of b1, b2, b3 and b4;
System.out.println( str1 );
System.out.println( str2 );
System.out.println( str3 );
System.out.println( str4 );
}
}
输出:
The first character 'A' is a letter or digit:true The second character '1' is a letter or digit:true The third character 'U' is a letter or digit:true The fourth character '2' is a letter or digit:true
例子2
public class JavaCharacterisLetterOrDigitExample2 {
public static void main(String[] args) {
// Create four char primitives ch1, ch2, ch3 and ch4.
char ch1;
char ch2;
char ch3;
char ch4;
// Assign the values to ch1, ch2, ch3 and ch4.
ch1 = 'A';
ch2 = '1';
ch3 = '*';
ch4 = '$';
// Create four boolean primitives b1, b2, b3 and b4.
boolean b1 = Character.isLetterOrDigit(ch1);
boolean b2 = Character.isLetterOrDigit(ch2);
boolean b3 = Character.isLetterOrDigit(ch3);
boolean b4 = Character.isLetterOrDigit(ch4);
String str1 = "The first character '"+ ch1+ "' is a letter or digit:" + b1;
String str2 = "The second character '"+ch2+"' is a letter or digit:" + b2;
String str3 = "The third character '"+ch3+ "' is a letter or digit:" + b3;
String str4 = "The fourth character '"+ch4+ "' is a letter or digit:" + b4;
// Printr the values of b1, b2, b3 and b4.
System.out.println( str1 );
System.out.println( str2 );
System.out.println( str3 );
System.out.println( str4 );
}
}
输出:
The first character 'A' is a letter or digit:true The second character '1' is a letter or digit:true The third character '*' is a letter or digit:false The fourth character '$' is a letter or digit:false
例子3
import java.util.Scanner;
public class JavaCharacterisLetterOrDigitExample3 {
public static void main(String[] args) {
// Ask for user input
System.out.print("Enter the first input:");
// Use the Scanner class to get the user input
Scanner s1 = new Scanner(System.in);
// Gets the user input
char[] value1 = s1.nextLine().toCharArray();
// Check whether the user input is a letter or not.
for (char ch1:value1) {
boolean result1 = Character.isLetterOrDigit(ch1);
// print the result
if(result1){
System.out.println("The character '" + ch1 + "' is a letter or digit. ");
}
else{
System.out.println("The character '" + ch1 + "' is neither a letter nor a digit.");
}
System.out.print("Enter the second input:");
Scanner s2 = new Scanner(System.in);
char[] value2 = s2.nextLine().toCharArray();
for (char ch2:value2) {
boolean result2 = Character.isLetterOrDigit(ch2);
if(result2){
System.out.println("The character '" + ch2 + "' is a letter or digit. ");
}
else{
System.out.println("The character '" + ch2 + "' is neither a letter nor a digit.");
}
System.out.print("Enter the third input:");
Scanner s3 = new Scanner(System.in);
char[] value3 = s3.nextLine().toCharArray();
for (char ch3:value3) {
boolean result3 = Character.isLetterOrDigit(ch3);
if(result2){
System.out.println("The character '" + ch3 + "' is a letter or digit. ");
}
else{
System.out.println("The character '" + ch3 + "' is neither a letter nor a digit.");
}
}
}
}
}
}
输出:
Enter the first input:R The character 'R' is a letter or digit. Enter the second input:9 The character '9' is a letter or digit. Enter the third input:A The character 'A' is a letter or digit.
Java 字符 isLetterOrDigit() 方法
Character 类的 isLetterOrDigit(int codePoint) 方法确定给定(或指定)的字符是字母还是数字或不是。
如果 Character.isLetter(int codePoint) 或 Character.isDigit(int codePoint) 方法为给定字符返回 true,则该字符被视为字母或数字。
用法
public static boolean isLetterOrDigit(int codePoint)
参数
codePoint:codePoint 是需要测试的字符。
返回值
isLetter(int codePoint) 方法返回一个布尔值,即如果给定(或指定)字符是字母或数字,则返回 true。否则,该方法返回 false。
示例 4
public class JavaCharacterisLetterOrDigitExample4{
public static void main(String[] args) {
// Create four char primitives codePoint1, codePoint2, codePoint3 and codePoint4.
int codePoint1;
int codePoint2;
int codePoint3;
int codePoint4;
// Assign the values to codePoint1, codePoint2, codePoint3 and codePoint4.
codePoint1 = 26;
codePoint2 = 134;
codePoint3= 198;
codePoint4 = 408;
// Create four boolean primitives b1, b2, b3 and b4.
boolean b1, b2, b3,b4;
// Check whether the codePoint1, codePoint2, codePoint3 and codePoint4 are letters or digits or not and assign the results to b1, b2, b3 and b4.
b1 = Character.isLetterOrDigit(codePoint1);
b2 = Character.isLetterOrDigit(codePoint2);
b3 = Character.isLetterOrDigit(codePoint3);
b4 = Character.isLetterOrDigit(codePoint4);
String str1 = "The first codePoint '"+codePoint1 + "' is a letter or a digit:" + b1;
String str2 = "The second codePoint '"+codePoint2 + "' is a letter or a digit:" + b2;
String str3 = "The third codePoint '"+codePoint3 + "' is a letter or a digit:" + b3;
String str4 = "The fourth codePoint '"+codePoint4 + "' is a letter or a digit:" + b4;
// Print the values of str1, str2, str3 and str4.
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
System.out.println(str4);
}
}
输出:
The first codePoint '26' is a letter or a digit:false The second codePoint '134' is a letter or a digit:false The third codePoint '198' is a letter or a digit:true The fourth codePoint '408' is a letter or a digit:true
例 5
import java.util.Scanner;
public class JavaChracterisLetterOrDigiExample5{
public static void main(String[] args) {
// Ask the user for first input.
System.out.print("Enter the first input:");
// Use the Scanner class to get the user input.
Scanner s1 = new Scanner(System.in);
// Gets the user input.
char[] value1 = s1.nextLine().toCharArray();
// Check whether the user input is a lettter or digit or not.
for (char ch1:value1) {
boolean result1 = Character.isLetterOrDigit(ch1);
// Print the first result.
if(result1){
System.out.println("The character '" + ch1 + "' is a letter or digit. ");
}
else{
System.out.println("The character '" + ch1 + "' is neither a letter nor digit.");
}
System.out.print("Enter the second input:");
Scanner s2 = new Scanner(System.in);
char[] value2 = s2.nextLine().toCharArray();
for (char ch2:value2) {
boolean result2 = Character.isLetterOrDigit(ch2);
if(result2){
System.out.println("The character '" + ch2 + "' is a letter or digit. ");
}
else{
System.out.println("The character '" + ch2 + "' is neither a letter nor digit.");
}
System.out.print("Enter the third input:");
Scanner s3 = new Scanner(System.in);
char[] value3 = s3.nextLine().toCharArray();
for (char ch3:value3) {
boolean result3 = Character.isLetterOrDigit(ch3);
if(result2){
System.out.println("The character '" + ch3 + "' is a letter or digit. ");
}
else{
System.out.println("The character '" + ch3 + "' is neither a letter nor digit.");
}
}
}
}
}
}
输出:
Enter the first input:A The character 'A' is a letter or digit. Enter the second input:6 The character '6' is a letter or digit. Enter the third input:^ The character '^' is a letter or digit..
相关用法
- Java Character isLetter()用法及代码示例
- Java Character isLowerCase()用法及代码示例
- Java Character isLowSurrogate()用法及代码示例
- Java Character isAlphabetic()用法及代码示例
- Java Character isValidCodePoint()用法及代码示例
- Java Character isISOControl()用法及代码示例
- Java Character isSpace()用法及代码示例
- Java Character isMirrored()用法及代码示例
- Java Character isBmpCodePoint()用法及代码示例
- Java Character isIdentifierIgnorable()用法及代码示例
- Java Character isDigit()用法及代码示例
- Java Character isJavaIdentifierPart()用法及代码示例
- Java Character isUpperCase()用法及代码示例
- Java Character isJavaLetterOrDigit()用法及代码示例
- Java Character isWhitespace()用法及代码示例
- Java Character isDefined()用法及代码示例
- Java Character isJavaIdentifierstart()用法及代码示例
- Java Character isHighSurrogate()用法及代码示例
- Java Character isSpaceChar()用法及代码示例
- Java Character codePointCount()用法及代码示例
注:本文由纯净天空筛选整理自 Java Character isLetterOrDigit() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。