Split a string, at every nth position

2020-01-25 07:27发布

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

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

where the 3 dots after the G indicates every nth position to split. In this case, the 3 dots indicate every 3 positions. An example:

Input: String st = "123124125134135145234235245"
Output: 123 124 125 134 135 145 234 235 245.

My question is, how do i let the user control the number of positions where the string must be split at? In other words, how do I make those 3 dots, n dots controlled by the user?

5条回答
Juvenile、少年°
2楼-- · 2020-01-25 07:41
private String[] StringSpliter(String OriginalString) {
    String newString = "";
    for (String s: OriginalString.split("(?<=\\G.{"nth position"})")) { 
        if(s.length()<3)
            newString += s +"/";
        else
            newString += StringSpliter(s) ;
    }
    return newString.split("/");
}
查看更多
男人必须洒脱
3楼-- · 2020-01-25 07:42

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"].
查看更多
地球回转人心会变
4楼-- · 2020-01-25 07:43

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() +")";
}
查看更多
一纸荒年 Trace。
5楼-- · 2020-01-25 08:01

For a big performance improvement, an alternative would be to use substring() in a loop:

public String[] splitStringEvery(String s, int interval) {
    int arrayLength = (int) Math.ceil(((s.length() / (double)interval)));
    String[] result = new String[arrayLength];

    int j = 0;
    int lastIndex = result.length - 1;
    for (int i = 0; i < lastIndex; i++) {
        result[i] = s.substring(j, j + interval);
        j += interval;
    } //Add the last bit
    result[lastIndex] = s.substring(j);

    return result;
}

Example:

Input:  String st = "1231241251341351452342352456"
Output: 123 124 125 134 135 145 234 235 245 6.

It's not as short as stevevls' solution, but it's way more efficient (see below) and I think it would be easier to adjust in the future, of course depending on your situation.


Performance tests (Java 7u45)

2,000 characters long string - interval is 3.

split("(?<=\\G.{" + count + "})") performance (in miliseconds):

7, 7, 5, 5, 4, 3, 3, 2, 2, 2

splitStringEvery() (substring()) performance (in miliseconds):

2, 0, 0, 0, 0, 1, 0, 1, 0, 0

2,000,000 characters long string - interval is 3.

split() performance (in miliseconds):

207, 95, 376, 87, 97, 83, 83, 82, 81, 83

splitStringEvery() performance (in miliseconds):

44, 20, 13, 24, 13, 26, 12, 38, 12, 13

2,000,000 characters long string - interval is 30.

split() performance (in miliseconds):

103, 61, 41, 55, 43, 44, 49, 47, 47, 45

splitStringEvery() performance (in miliseconds):

7, 7, 2, 5, 1, 3, 4, 4, 2, 1

Conclusion:

The splitStringEvery() method is a lot faster (even after the changes in Java 7u6), and it escalates when the intervals become higher.

Ready-to-use Test Code:

pastebin.com/QMPgLbG9

查看更多
兄弟一词,经得起流年.
6楼-- · 2020-01-25 08:04

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.

查看更多
登录 后发表回答