Split a string, at every nth position

后端 未结 5 930
一个人的身影
一个人的身影 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:18

    You can use the brace operator to specify the number of times a character must occur:

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

    The brace is a handy tool because you can use it to specify either an exact count or ranges.

提交回复
热议问题