如何将包含代理对字符和普通字符的 unicode 字符串拆分为 List<String>
字符数?
(需要 String
来存储由两个 char
组成的代理对字符)
最佳答案
试试这个。
String s = "😊a👦c😊";
List<String> result = List.of(s.split("(?<=.)"));
for (String e : result)
System.out.println(e + " : length=" + e.length());
输出:
😊 : length=2
a : length=1
👦 : length=2
c : length=1
😊 : length=2
代码点
或者,使用 code point 的流整数。
List<String> result =
s
.codePoints() // Produce a `IntStream` of code point numbers.
.mapToObj(Character::toString) // Produce a `String` containing one or two java chars for each code point in the stream.
.collect(Collectors.toList());
查看此 code run live at IdeOne.com .
要捕获代码点,请使用上述代码的这种变体。
List<Integer> codePointNumbers =
s
.codePoints()
.boxed()
.collect( Collectors.toList() ) ;
运行时:
codePointNumbers.toString(): [128522, 97, 128102, 99, 128522]
关于java - 将 unicode 字符串拆分为字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66788598/