Java中码点和码点单元及码点与字符串的互化

it2022-05-09  40

码点,就是某个任意字符在Unicode编码表中对应的代码值

代码单元:是在计算机中用来表示码点的,大部分码点只需要一个代码单元表示,但是有一些是需要两个代码单元表示的。


遍历一个字符串,依次将每一个码点存入数组并输出,再用数组中的码点转化回字符串

(1)方法如下:

int[]codePoints=str.codePoints().toArray(); str=new String(codePoints,0,codePoints.length);

(2)代码如下:

//此处以字符串'Hello'为例 public class Pratice { public static void main(String[] args) { String str = "Hello"; int i; int[] codePoints = str.codePoints().toArray(); str = new String(codePoints, 0, codePoints.length); for (i = 0; i < codePoints.length; i++) System.out.println(codePoints[i]); System.out.println(str); } }

以上程序本人已调试完毕,若程序有繁杂之处,欢迎批评指正!

欢迎大家相互关注交流! 我的个人主页:GershonHold个人博客


最新回复(0)