简体   繁体   中英

String.split() is not working

I have to split a string in this way :

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

The correct result is:

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

And this is working in Java, but in Android it returns:

["JS", "1B4BJG2C2G3S6C6S5CHG1C4CKS3C1G5S6B3G7BHB7G6G5B"]

...as I can see when debugging:

在此处输入图片说明

Any ideas?

Try using this expression ( G.. instead of G.{2} ):

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

Or you can appending space every two step ( using as delimiter ) :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM