charAt()方法排坑
String类提供的charAt方法可以很容易的获取一个字符串对应索引的位置的字符
public static void main(String[] args) {
String str = "Hello";
System.out.println(str.charAt(0)); //H
}
但是我们知道有一些Unicode编码采用的是双编码的形式,例如😀这个emoj表情使用\uD83D \uDE00 表示,这时候我们使用charAt就会出现问题,下边返回的是笑脸的第一个编码单元
public static void main(String[] args) {
String str = "😀Hello";
System.out.println(str.charAt(0)); //?
System.out.println(str.charAt(2)); //H
}
为了解决上述问题,我们可以采用码点的方式进行解决,
public class Main {
public static void main(String[] args) {
String greeting = "😀is a dog";
int index = greeting.offsetByCodePoints(0,0);
System.out.println(greeting.codePointAt(index)); //输出对应的码点
System.out.println( Character.toChars(greeting.codePointAt(index))); //输出码点对应的字符
}
}
public int offsetByCodePoints(int index, int codePointOffset) 是 Java 中的一个方法,它用于在字符序列中移动指定数量的代码点。方法接受两个参数:
index:指定起始索引,即要从哪个位置开始移动;codePointOffset:指定要移动的代码点数量,可以是正数(向后移动)或负数(向前移动)。
当我们获取对应的码点后就可以通过toChars将其转换为对应的字符,也可以采用该方式进行遍历。
public class Main {
public static void main(String[] args) {
String greeting = "\uD83D\uDE00 is a dog";
int i = 0;
while(i<greeting.codePointCount(0,greeting.length())){
int index = greeting.offsetByCodePoints(0,i);
System.out.print( Character.toChars(greeting.codePointAt(index)));
i++;
}
}
}
codePointCount()方法用于返回指定位置的码点数量