String.split()不起作用

时间:2017-12-31 17:12:06

标签: java android

我必须以这种方式分割字符串:

String s = "00JS1B4BJG2C2G3S6C6S5CHG1C4CKS3C1G5S6B3G7BHB7G6G5B";
String[] result = s.substring(2).split("(?<=\\G.{2})");

正确的结果是:

["JS","1B","4B","JG","2C","2G","3S","6C","6S","5C","HG","1C","4C","KS","3C","1G","5S","6B","3G","7B","HB","7G","6G","5B"]

这适用于Java,但在Android中它返回:

["JS", "1B4BJG2C2G3S6C6S5CHG1C4CKS3C1G5S6B3G7BHB7G6G5B"]

......正如我在调试时看到的那样:

enter image description here

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

尝试使用此表达式(G..代替G.{2}):

s.split("(?<=\\G..)")

或者您可以每两步添加一次空格(使用as分隔符):

s = s.replaceAll("..(?!$)", "$0 ");
s.split(" ");