在Java中处理Unicode字符非常简单,因为Java的字符串是以Unicode编码的
\u
表示法:public class UnicodeExample {
public static void main(String[] args) {
String unicodeString = "\u4F60\u597D"; // 你好
System.out.println(unicodeString);
}
}
char
数据类型表示Unicode字符:public class UnicodeExample {
public static void main(String[] args) {
char ch1 = '\u4F60'; // 你
char ch2 = '\u597D'; // 好
System.out.println(ch1 + "" + ch2);
}
}
Character
类处理Unicode字符:public class UnicodeExample {
public static void main(String[] args) {
char ch1 = '\u4F60'; // 你
char ch2 = '\u597D'; // 好
int codePoint1 = Character.codePointAt(new char[]{ch1}, 0);
int codePoint2 = Character.codePointAt(new char[]{ch2}, 0);
System.out.println("Code point of " + ch1 + ": " + codePoint1);
System.out.println("Code point of " + ch2 + ": " + codePoint2);
}
}
String
类处理Unicode字符:public class UnicodeExample {
public static void main(String[] args) {
String unicodeString = "\u4F60\u597D"; // 你好
int length = unicodeString.length();
for (int i = 0; i< length; i++) {
char ch = unicodeString.charAt(i);
int codePoint = Character.codePointAt(unicodeString, i);
System.out.println("Character: " + ch + ", Code point: " + codePoint);
}
}
}
这些示例展示了如何在Java中处理Unicode字符。你可以根据需要选择合适的方法来处理Unicode字符。