title: Splitting a string into n-length chunks in Java
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/3760152/split-string-to-equal-length-substrings-in-java">Split string to equal length substrings in Java</a>  </p>
  </blockquote>
  
  
  
  <p>Given the following utility method I have:</p>
  
  <pre><code>/**
   * Splits string &lt;tt&gt;s&lt;/tt&gt; into chunks of size &lt;tt&gt;chunkSize&lt;/tt&gt;
   *
   * @param s the string to split; must not be null
   * @param chunkSize number of chars in each chuck; must be greater than 0
   * @return The original string in chunks
   */
  public static List&lt;String&gt; splitInChunks(String s, int chunkSize) {
      Preconditions.checkArgument(chunkSize &gt; 0);
      List&lt;String&gt; result = Lists.newArrayList();
      int length = s.length();
      for (int i = 0; i &lt; length; i += chunkSize) {
          result.add(s.substring(i, Math.min(length, i + chunkSize)));
      }
      return result;
  }
  </code></pre>
  
  <p>1) <strong>Is there an equivalent method in any common Java library</strong> (such as Apache Commons, Google Guava) so  I could throw it away from my codebase? Couldn't find with a quick look. Whether it returns an array or a List of Strings doesn't really matter.</p>
  
  <p>(Obviously I wouldn't add dependency to some huge framework just for this, but feel free to mention any common lib; maybe I use it already.) </p>
  
  <p>2) <strong>If not, is there some simpler and cleaner way to do this in Java?</strong> Or a way that is strikingly more performant? (If you suggest a regex-based solution, please also consider cleanness in the sense of readability for non regex experts... :-)</p>
  
  <p><strong>Edit</strong>: this qualifies as a duplicate of the question "<a href="https://stackoverflow.com/questions/3760152/split-string-of-equal-lengths-in-java">Split string to equal length substrings in Java</a>"  because of <strong><a href="https://stackoverflow.com/questions/3760152/split-string-to-equal-length-substrings-in-java/3760862#3760862">this Guava solution</a></strong> which perfectly answers my question!</p>