Split a string, at every nth position

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

    If you want to build that regex string you can set the split length as a parameter.

    public String getRegex(int splitLength)
    {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < splitLength; i++)
            builder.append(".");
    
        return "(?<=\\G" + builder.toString() +")";
    }
    

提交回复
热议问题