Java String Split by Length splitEqually(String _text, int _len)

Here you can find the source of splitEqually(String _text, int _len)

Description

Splits a Text to equal parts.

License

Open Source License

Parameter

Parameter Description
_text text to split
_len max length per line

Return

list of string splitted to _len or null if _text was null

Declaration

public static List<String> splitEqually(String _text, int _len) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**//from   w  w  w  .  j a v a 2 s. c  o  m
     * Splits a Text to equal parts.
     * There is no detection of words, everything will be cut to the same length.
     *
     * @param _text text to split
     * @param _len max length per line
     * @return list of string splitted to _len or null if _text was null
     */
    public static List<String> splitEqually(String _text, int _len) {
        if (_text == null) {
            return null;
        }
        List<String> ret = new ArrayList<String>((_text.length() + _len - 1) / _len);

        for (int start = 0; start < _text.length(); start += _len) {
            ret.add(_text.substring(start, Math.min(_text.length(), start + _len)));
        }
        return ret;
    }
}

Related

  1. splitByLength(String string, int len)
  2. splitByteArray(byte[] inByteArray, int length)
  3. splitByteArray(final byte[] data, final int packetLength)
  4. splitByTrimmedDelimiterNonNestedInBracketsOrQuotesOrComments(final String s, final char delimiter, final char quoteChar, char bracketChar, final boolean includeIntermediaryEmptyElmts, final int maxElmts, String cmtStart, String cmtEnd)
  5. splitByWholeSeparatorWorker(final String str, final String separator, final int max, final boolean preserveAllTokens)
  6. splitEscapedString(String str, char sep, char esc, int maxPart)
  7. splitFilenames(String text)
  8. splitHtmlString(String stringToSplit, int maxLength)
  9. splitIntoFrames(String src, int maxFrameSize)