Java Character类用法代码示例大全

下面我将展示Java中Character类的各种用法,涵盖其主要方法和功能。

1. 基本创建和转换

// 创建Character对象
Character ch1 = Character.valueOf('A'); // 推荐方式
Character ch2 = 'B'; // 自动装箱
Character ch3 = new Character('C'); // 不推荐(Java 9后废弃)

// 转换为基本类型
char c1 = ch1.charValue();
char c2 = ch2; // 自动拆箱

System.out.println("ch1: " + ch1); // A
System.out.println("c1: " + c1);   // A

2. 字符分类方法

// 检查字符类型
System.out.println(Character.isLetter('A'));      // true
System.out.println(Character.isDigit('5'));       // true
System.out.println(Character.isWhitespace(' '));  // true
System.out.println(Character.isUpperCase('Z'));   // true
System.out.println(Character.isLowerCase('z'));   // true
System.out.println(Character.isLetterOrDigit('_')); // false
System.out.println(Character.isJavaIdentifierStart('$')); // true
System.out.println(Character.isJavaIdentifierPart('1')); // true
System.out.println(Character.isISOControl('\u0007')); // true (控制字符)

3. 字符转换方法

// 大小写转换
System.out.println(Character.toUpperCase('a')); // A
System.out.println(Character.toLowerCase('Z')); // z

// 数字和字符转换
System.out.println(Character.digit('7', 10));  // 7
System.out.println(Character.digit('A', 16)); // 10 (十六进制)
System.out.println(Character.forDigit(10, 16)); // 'a'
System.out.println(Character.forDigit(15, 16)); // 'f'

4. Unicode相关操作

// Unicode基本属性
System.out.println(Character.MIN_VALUE);     // 0
System.out.println(Character.MAX_VALUE);     // 65535
System.out.println(Character.MIN_CODE_POINT); // 0
System.out.println(Character.MAX_CODE_POINT); // 1114111

// 获取Unicode类别
System.out.println(Character.getType('A'));  // 1 (UPPERCASE_LETTER)
System.out.println(Character.getType(' '));  // 12 (SPACE_SEPARATOR)
System.out.println(Character.getType('9'));  // 9 (DECIMAL_DIGIT_NUMBER)

// 比较字符
System.out.println(Character.compare('A', 'B')); // -1
System.out.println(Character.compare('B', 'A')); // 1
System.out.println(Character.compare('A', 'A')); // 0

5. 代理对和补充字符处理

// 处理补充字符(代码点大于0xFFFF的字符)
int smileyCodePoint = 0x1F600; // 😀表情符号
char[] surrogatePair = Character.toChars(smileyCodePoint);
System.out.println("Surrogate pair length: " + surrogatePair.length); // 2

// 检查代理对
char highSurrogate = surrogatePair[0];
char lowSurrogate = surrogatePair[1];
System.out.println(Character.isHighSurrogate(highSurrogate)); // true
System.out.println(Character.isLowSurrogate(lowSurrogate));  // true
System.out.println(Character.isSurrogatePair(highSurrogate, lowSurrogate)); // true

// 从代理对获取代码点
int codePoint = Character.toCodePoint(highSurrogate, lowSurrogate);
System.out.println(codePoint == smileyCodePoint); // true
System.out.println(Character.charCount(codePoint)); // 2

6. 字符串中的字符处理

String str = "Hello 世界 😊";

// 遍历字符串中的代码点(正确处理补充字符)
int length = str.codePointCount(0, str.length());
for (int i = 0; i < length; i++) {
    int codePoint = str.codePointAt(str.offsetByCodePoints(0, i));
    System.out.printf("U+%04X %s%n", codePoint, 
            Character.isSupplementaryCodePoint(codePoint) ? "(补充字符)" : "");
}

// 输出:
// U+0048 
// U+0065 
// U+006C 
// U+006C 
// U+006F 
// U+0020 
// U+4E16 (补充字符)
// U+754C (补充字符)
// U+0020 
// U+1F60A (补充字符)

7. 数字转换和进制处理

// 不同进制下的数字转换
System.out.println(Character.digit('F', 16)); // 15
System.out.println(Character.digit('8', 8));  // -1 (无效)
System.out.println(Character.digit('7', 8));  // 7

// 数字到字符转换
System.out.println(Character.forDigit(10, 16)); // 'a'
System.out.println(Character.forDigit(15, 16)); // 'f'
System.out.println(Character.forDigit(9, 10));  // '9'

8. 其他实用方法

// 方向性检查(用于双向文本)
System.out.println(Character.getDirectionality('A')); // 0 (LTR)
System.out.println(Character.getDirectionality('א')); // 1 (RTL)

// 字节顺序反转
char original = 0xABCD;
char reversed = Character.reverseBytes(original);
System.out.printf("Original: 0x%04X, Reversed: 0x%04X%n", 
        (int)original, (int)reversed);
// 输出: Original: 0xABCD, Reversed: 0xCDAB

9. 字符流处理示例

// 过滤字符串中的非字母字符
String input = "Hello123World!@#";
StringBuilder filtered = new StringBuilder();

input.chars()
    .filter(c -> Character.isLetter(c))
    .forEach(c -> filtered.append((char)c));

System.out.println(filtered.toString()); // HelloWorld

10. 性能比较

// 直接使用char vs Character方法
long start, end;
char testChar = 'A';

// 直接char操作
start = System.nanoTime();
for (int i = 0; i < 1000000; i++) {
    boolean b = (testChar >= 'A' && testChar <= 'Z');
}
end = System.nanoTime();
System.out.println("char直接操作: " + (end - start) + " ns");

// Character方法
start = System.nanoTime();
for (int i = 0; i < 1000000; i++) {
    boolean b = Character.isUpperCase(testChar);
}
end = System.nanoTime();
System.out.println("Character方法: " + (end - start) + " ns");

这些示例涵盖了Character类的主要用法,包括字符分类、转换、Unicode处理、代理对操作等。在实际开发中,应根据具体需求选择合适的方法。

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容