在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字符。