Java Utililty Methods String Split by Length

List of utility methods to do String Split by Length

Description

The list of methods to do String Split by Length are organized into topic(s).

Method

ListsplitString(String s, int maxSize)
split String
List<String> linesOfMaxSize = new ArrayList<String>();
int i = 0;
while (i < s.length()) {
    linesOfMaxSize.add(s.substring(i, Math.min(i + maxSize, s.length())));
    i += maxSize;
return linesOfMaxSize;
ListsplitString(String str, int length)
split String
List list = new ArrayList<String>();
return list;
ListsplitString(String str, int length)
split String
List<String> list = new ArrayList<String>();
for (int i = 0; i < str.length(); i += length) {
    int endIndex = i + length;
    if (endIndex <= str.length()) {
        list.add(str.substring(i, i + length));
    } else {
        list.add(str.substring(i, str.length() - 1));
return list;
ListsplitString(String string, int maxCharsPerLine)
Splits a string into multiple lines, each with at most maxChars characters.
final List<String> lines = new ArrayList<String>();
final String[] items = string.split(" ");
final StringBuilder currString = new StringBuilder();
for (String item : items) {
    if (currString.length() + item.length() > maxCharsPerLine) {
        lines.add(currString.toString());
        currString.setLength(0); 
    currString.append(item + " ");
lines.add(currString.toString());
return lines;
ListsplitString(String stringToSplit, int maxLength)
Split a line of text in lines no longer than maxLength characters.
String text = stringToSplit;
List<String> lines = new ArrayList<String>();
while (text.length() > maxLength) {
    int spaceAt = maxLength - 1;
    for (int i = maxLength - 1; i > 0; i--) {
        if (Character.isWhitespace(text.charAt(i))) {
            spaceAt = i;
            break;
...
String[]splitStringByLength(String input, int length)
split String By Length
List<String> output = new ArrayList<>();
while (input.length() > length) {
    output.add(input.substring(0, length));
    input = input.substring(length);
if (input.length() > 0) {
    output.add(input);
return output.toArray(new String[output.size()]);
String[]splitStringFixedLen(String data, int interval)
split String Fixed Len
List<String> dataPiece = new ArrayList<String>();
int addedOffset;
for (int offset = 0; offset < data.length(); offset += addedOffset) {
    String subData = data.substring(offset, Math.min(data.length(), (offset + interval)));
    addedOffset = subData.lastIndexOf('\n');
    if (addedOffset >= 0) {
        subData = subData.substring(0, addedOffset);
        ++addedOffset;
...
String[]splitWorker(final String str, final String separatorChars, final int max, final boolean preserveAllTokens)
Performs the logic for the split and splitPreserveAllTokens methods that return a maximum array length.
if (str == null) {
    return null;
final int len = str.length();
if (len == 0) {
    return new String[0];
final List<String> list = new ArrayList<>();
...
ListstrSplit(String src, int firstLineLength, int otherLineLength)
str Split
List<String> result = new ArrayList<>();
String subStr = null;
if (firstLineLength < src.length()) {
    subStr = src.substring(0, firstLineLength);
    result.add(subStr);
    for (int i = firstLineLength; i < src.length(); i += otherLineLength) {
        if (i + otherLineLength > src.length()) {
            subStr = src.substring(i, src.length());
...