Java String Chop chop(String string, int length)

Here you can find the source of chop(String string, int length)

Description

chop up strings to chunks of the specified length (last chunk may be shorter)

License

Apache License

Declaration

public static String[] chop(String string, int length) 

Method Source Code

//package com.java2s;
/*/*from  www  . jav  a 2 s  .c  o  m*/
 *  Copyright Gergely Nagy <greg@webhejj.hu>
 *
 *  Licensed under the Apache License, Version 2.0; 
 *  you may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 */

public class Main {
    /**
     * chop up strings to chunks of the specified length (last chunk may be shorter)
     */
    public static String[] chop(String string, int length) {
        if (string.length() == 0) {
            return new String[0];
        }
        int chunkCount = string.length() / length + 1;
        String[] chunks = new String[chunkCount];

        for (int i = 0; i < chunkCount; i++) {
            chunks[i] = string.substring(i * length, Math.min(string.length(), (i + 1) * length));
        }
        return chunks;
    }
}

Related

  1. chop(String str)
  2. chop(String str)
  3. chop(String str)
  4. chop(String str)
  5. chop(String str, int maxLength)
  6. chop(String string, int length)
  7. chop(String value, int size)
  8. chop(String x)
  9. chop(String[] arr, int pos)