Java Character isLetterOrDigit() 方法

2024年11月6日 | 阅读 6 分钟

Character 类的 isLetterOrDigit(char ch) 方法用于确定给定的(或指定的)字符是否是字母或数字。

如果给定的字符满足 Character.isLetter(char ch) 或 Character.isDigit(char ch) 方法返回 true,则该字符被认为是字母或数字。

注意:上述方法不能用于处理增补字符。为了处理增补字符,我们可以使用 isLetterOrDigit(int) 方法。

语法

参数

上述方法只需要一个参数

a.) 需要测试的字符。

返回值

isLetterOrDigit(char ch) 方法返回一个布尔值,即如果给定的(或指定的)字符是字母或数字,则返回 true。否则,该方法返回 false。

示例 1

输出

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

输出

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

输出

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 Character isLetterOrDigit() 方法

Character 类的 isLetterOrDigit(int codePoint) 方法用于确定给定的(或指定的)字符是否是字母或数字。

如果给定的字符满足 Character.isLetter(int codePoint) 或 Character.isDigit(int codePoint) 方法返回 true,则该字符被认为是字母或数字。

语法

参数

codePoint:需要测试的字符的 codePoint。

返回值

isLetter(int codePoint) 方法返回一个布尔值,即如果给定的(或指定的)字符是字母或数字,则返回 true。否则,该方法返回 false。

示例 4

输出

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

输出

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..