Split a string, at every nth position

后端 未结 5 931
一个人的身影
一个人的身影 2020-11-28 10:14

I use this regex to split a string at every say 3rd position:

String []thisCombo2 = thisCombo.split(\"(?<=\\\\G...)\");

where the 3 dots

5条回答
  •  旧巷少年郎
    2020-11-28 11:17

    Using Google Guava, you can use Splitter.fixedLength()

    Returns a splitter that divides strings into pieces of the given length

    Splitter.fixedLength(2).split("abcde");
    // returns an iterable containing ["ab", "cd", "e"].
    

提交回复
热议问题