Java Character 类

12 Dec 2024 | 8 分钟阅读

Character 类通常将所有原始类型 char 的值包装到一个对象中。Character 类型的任何对象可能包含一个类型为 char 的单个字段。

Character 类的所有字段、方法和构造函数都由 Unicode 数据文件指定,该文件是 Unicode 字符数据库(Unicode Character Database)的一部分,并由 Unicode 联盟维护。

从 U+0000 到 U+FFFF 的字符集有时被称为基本多语言平面(Basic Multilingual Plane,即 BMP)。代码点大于 U+FFFF 的字符被称为补充字符。Java 语言通常使用 UTF-16 编码方法来表示 String 或 String Buffer 中的 char 数组。在这种表示形式中,补充字符表示为一对字符,第一个来自高代理范围(\uD800-\uDBFF),第二个来自低代理范围(\uDc00-\uDBFF)。

Java Character 类的方法

方法描述
charCount(int codePoint)确定表示给定字符所需的 char 值数量。
charValue()返回给定 Character 对象的 char 值。
codePointAt(char[]a, int index)返回给定数组指定索引处的 codePoint。
codePointAt(char[]a, int index, int limit )返回指定索引处的 char 数组的 codePoint,其中只使用索引小于指定 limit 的数组元素。
codePointAt(CharSequence seq, int index)返回给定 CharSequence 指定索引处的 codePoint。
codePointBefore(char[]a, int index)返回给定数组前一个索引处的 codePoint。
codePointBefore(char[]a, int index, int start)返回给定数组前一个索引处的 codePoint,其中只使用索引大于或等于 start 的数组元素。
codePointBefore(CharSequence, int index)返回给定 CharSequence 前一个索引处的 CodePoint。
codePointCount(char[]a, int offset, int count)返回给定 char 数组的子数组中 Unicode codePoint 的总数。
codePointCount(CharSequence seq, int beginIndex, int endIndex)返回给定 char 序列在指定文本范围内的 Unicode codePoint 数量。
codePointOf(String name)返回给定 Unicode 字符(由特定字符名称指定)的 codePoint 值。
compare(char x, char y)按数值比较两个 char 类型的值。
compareTo(Character anotherCharacter)按数值比较两个 Character 类型对象。
digit(char ch, int radix)返回指定基数中给定字符的数字值。
digit(int codePoint, int radix)返回指定基数中给定字符的数字值。
equals(object obj)将给定对象与指定对象进行比较。
forDigit(int digit, int radix)确定给定基数中特定数字的字符表示形式。
getDirectionality(char ch)返回指定字符的 Unicode 方向性属性。
getDirectionality(int codePoint)返回指定 codePoint 的 Unicode 方向性属性。
getName(int codePoint)返回给定 codePoint 的 Unicode 名称,如果 codePoint 未分配,则可能返回 null。
getNumericValue(char ch)返回指定 Unicode 字符的整数类型值。
getNumericValue(int codePoint)返回指定 Unicode codePoint 的整数类型值。
getType(char ch)返回指示字符一般类别的数值。
getType(int codePoint)返回指示字符一般类别的数值。
hashCode()返回指定字符的哈希码。此方法等于调用 charValue() 方法的结果。
hashCode(char value)返回特定 char 值的哈希码。
highSurrogate(int codePoint)返回代理对的前导代理 codePoint,它在 UTF-16 编码中表示补充字符。
isAlphabetic(int codePoint)确定给定字符是否为字母。
isBmpCodePoint(int codePoint)确定给定字符是否位于基本多语言平面 (BMP) 中。
isDefined(char ch)确定字符是否在 Unicode 中定义。
isDefined(int codePoint)确定字符(Unicode code point)是否在 Unicode 中定义。
isDigit(char ch)确定给定字符是否为数字。
isDigit(int codePoint)确定给定字符(Unicode code point)是否为数字。
isHighSurrogate(char ch)确定给定 char 值是否为 Unicode 高代理 code point。
isIdentifierIgnorable(char ch)确定给定字符是否被视为 Java 标识符中的可忽略字符。
isIdentifierIgnorable(int codePoint)确定给定字符(Unicode code point)是否被视为 Java 标识符中的可忽略字符。
isIdeographic(int codePoint)确定指定字符是否为 CJKV(中日韩越)表意文字。
isISOControl(char ch)确定给定字符是否为 ISO 控制字符。
isISOControl(int codePoint)确定给定字符(Unicode code point)是否为 ISO 控制字符。
isJavaIdentifierPart(char ch)确定给定字符是否是 Java 标识符的一部分(第一个字符除外)。
isJavaIdentifierPart(int codePoint)确定给定字符(Unicode code point)是否是 Java 标识符的一部分(第一个字符除外)。
isJavaIdentifierStart(char ch)确定给定字符是否可以作为 Java 标识符的第一个字符。
isJavaIdentifierStart(int codePoint)确定给定字符(Unicode code point)是否可以作为 Java 标识符的第一个字符。
isJavaLetter(char ch)已被 isJavaIdentifierStart(char) 替换。
isJavaLetterOrDigit(int codePoint)已被 isJavaIdentifierPart(char) 替换。
isLetter(char ch)确定指定字符是否为字母。
isLetter(int codePoint)确定指定字符(Unicode code point)是否为字母。
isLetterOrDigit(char ch)确定指定字符是否为字母或数字。
isLetterOrDigit(int codePoint)确定指定字符(Unicode code point)是否为字母或数字。
isLowerCase(char ch)确定指定字符是否为小写字符。
isLowerCase(int codePoint)确定指定字符(Unicode code point)是否为小写字符。
isLowSurrogate(char ch)确定给定 char 值是否为 Unicode 低代理 code unit。
isMirrored(char ch)确定字符是否为镜像字符。
isMirrored(int codePoint)确定字符(Unicode code point)是否为镜像字符。
isSpace(char ch)已被 isWhiteSpace(char ch) 方法替换。
isSpaceChar(char ch)确定指定字符是否为 Unicode 空格字符。
isSupplementaryCodePoint(int codePoint)确定给定字符(Unicode code point)是否在补充字符的范围内。
isSurrogate(char ch)确定给定 char 值是否为 Unicode 代理 code unit。
isSurrogatePair(char high, char low)确定给定 char 值对是否为有效的 Unicode 代理对。
isTitleCase(char ch)确定给定字符是否为 titlecase 字符。
isTitleCase(int codePoint)确定给定字符(Unicode code point)是否为 titlecase 字符。
isUnicodeIdentifierPart(char ch)确定给定字符是否是 Unicode 标识符的一部分。
isUnicodeIdentifierPart(int codePoint)确定给定字符(Unicode code point)是否是 Unicode 标识符的一部分。
isUnicodeIdentifierStart(char ch)确定给定字符是否可以作为 Unicode 标识符的第一个字符。
isUnicodeIdentifierStart(int codePoint)确定给定字符(Unicode code point)是否可以作为 Unicode 标识符的第一个字符。
isUpperCase(char ch)确定给定字符是否为大写字符。
isUpperCase(int codePoint)确定给定字符(Unicode code point)是否为大写字符。
isValidCodePoint(int codePoint)确定给定字符是否为有效的 Unicode code point 值。
isWhitespace(char ch)确定给定字符是否为空白字符。
isWhitespace(int codePoint)确定给定字符(Unicode code point)是否为空白字符。
lowSurrogate(int codePoint)返回代理对的低代理 code unit(即 trailing surrogate),它在 UTF-16 编码中表示补充字符。
offsetByCodePoints(char[]a, int start, int count, int index, int codePointOffset)返回给定 char 子数组中的索引,该索引是从给定索引偏移 codePointOffset 个 codePoint 得到的。
offsetByCodePoints(CharSequence seq, int index, int codePointOffset)返回给定 char 子数组中的索引,该索引是从给定索引偏移 codePointOffset 个 codePoint 得到的。
reverseBytes(char ch)返回通过反转指定 char 值中字节顺序而获得的值。
toChars(int codePoint)将指定的字符(Unicode code point)转换为其 UTF-16 表示形式,该形式存储在 char 数组中。
toChars(int codePoint, char[] dst, int dstIndex)将指定的字符(Unicode code point)转换为其 UTF-16 表示形式。
toCodePoint(char high, char low)将给定的代理对转换为其对应的补充 code point 值。
toLowerCase(char ch)使用从 Unicode 数据文件中获得的有关大小写映射的信息,将 char 类型参数转换为小写。
toLowerCase(int codePoint)使用从 Unicode 数据文件中获得的有关大小写映射的信息,将 char 类型参数(Unicode code point)转换为小写。
toString()返回表示字符值的 String 类型对象。
toString(char ch)返回表示指定字符的 String 类型对象。
toTitleCase(char ch)使用从 Unicode 数据文件中获得的有关大小写映射的信息,将 char 类型参数转换为 titlecase。
toTitleCase(int codePoint)使用从 Unicode 数据文件中获得的有关大小写映射的信息,将 char 类型参数(Unicode code point)转换为小写。
toUpperCase(char ch)使用从 Unicode 数据文件中获得的有关大小写映射的信息,将 char 类型参数转换为大写。
toUpperCase(int codePoint)使用从 Unicode 数据文件中获得的有关大小写映射的信息,将 char 类型参数(Unicode code point)转换为大写。
valueOf(char c)返回代表指定 char 值的 Character 实例。

示例 1

输出

Enter the first input:89
The value comes to: 1
Enter the second input:J
The hash code for the character 'J' is given as:74
Enter the third input:5
The character '5' is a digit. 
Enter the fourth input:h
The fourth character 'h' is an ISO Control:false

示例 2

输出

The first character is a letter: true
 The second character is a lower-case: true
 The third character has a space: true
 The third character has a space: true