Split string to equal length substrings in Java

后端 未结 21 2170
日久生厌
日久生厌 2020-11-22 02:56

How to split the string \"Thequickbrownfoxjumps\" to substrings of equal size in Java. Eg. \"Thequickbrownfoxjumps\" of 4 equal size should give th

21条回答
  •  独厮守ぢ
    2020-11-22 03:25

    If you're using Google's guava general-purpose libraries (and quite honestly, any new Java project probably should be), this is insanely trivial with the Splitter class:

    for (String substring : Splitter.fixedLength(4).split(inputString)) {
        doSomethingWith(substring);
    }
    

    and that's it. Easy as!

提交回复
热议问题