我需要将一个可能包含表情符号的字符串拆分为单个字符的列表(保留表情符号)。目前,任何表情符号都会被拆分成它的各个部分,这是可以预见的情况。
String s = "abc";
String[] tokens = s.split("");
// tokens is ["?","?","?","?","a","b","c","?","?"]
// tokens should be ["","","a","b","c",""]
我希望尽可能减少项目的大小并尽量少依赖,因此我想远离任何第三方库。确切的输出类型并不太重要,只要我能按顺序遍历标记就可以了。
List<String> results = Pattern.compile("\\P{M}\\p{M}*+").matcher(s).results().map(MatchResult::group).collect(Collectors.toList());
。请参见demo。 - Wiktor Stribiżews.replaceAll("\\p{So}|.", "$0\0").split("\0+");
这段代码也适用于你。 - anubhava