Java Character isDigit() 方法

2024年11月6日 | 阅读时长 5 分钟

Character 类的 isDigit(int codePoint) 方法通常用于确定给定的字符是否为数字。通常,如果字符的通用类别(由 getType(codePoint) 提供)是 DECIMAL_DIGIT_NUMBER,则该字符被视为数字。

语法

参数

上述方法只需要一个参数

a.) codePoint 是需要测试的 Unicode 字符。

返回值

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

示例 1

输出

The codePoint '55' is a digit.
The codePoint '32' is not a digit.
The codePoint '121' is not a digit.
The codePoint '49' is a digit.
The codePoint '200' is not a digit.

示例 2

输出

The first object represents a digit :  true
The first object represents a digit :  false
The first object represents a digit :  false
The first object represents a digit :  false

Java Character isDigit() 方法

Character 类的 isDigit(char ch) 方法通常用于确定给定的字符是否为数字。

通常,如果字符的通用类别(由 Character.getType(ch) 提供)是 DECIMAL_DIGIT_NUMBER,则该字符被视为数字。

注意:上述方法不能用于处理增补字符。为了支持所有 Unicode 字符,包括所有增补字符,我们可以使用 isdigit(int) 方法。

语法

参数

上述方法只需要一个参数

a.) 需要测试的字符。

返回值

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

示例 3

输出

The digit ' ' is:false
The digit '4' is:true
The digit '8' is:true

示例 4

输出

Enter the first character: &
The character '&' is not a digit.
Enter the first character: 3
The character '3' is a digit.

示例 5

输出

The digit 'A' is:false
The digit '3' is:true
The digit 'b' is:false
The digit '6' is:true