Java String Split by Length splitByLength(String string, int len)

Here you can find the source of splitByLength(String string, int len)

Description

split By Length

License

Apache License

Declaration

public static String[] splitByLength(String string, int len) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;

public class Main {
    public static String[] splitByLength(String string, int len) {
        if (string == null || len <= 0)
            return null;

        if (string.length() < len) {
            String[] arr = new String[1];
            arr[0] = string;//from w ww.j a  v a  2 s  . c  om
            return arr;
        }

        int chunks = string.length() / len + ((string.length() % len > 0) ? 1 : 0);
        String[] arr = new String[chunks];
        for (int i = 0, j = 0, l = string.length(); i < l; i += len, j++)

            // int xl = ((len > l-i) ? l-i : len)
            arr[j] = string.substring(i, (i + ((len > l - i) ? l - i : len)));
        return arr;
    }

    public static ArrayList<String> splitByLength(String string, int len, boolean splitAtNewLine, char Justify) {
        if (string == null || len <= 0)
            return (ArrayList<String>) null;

        String fmt = "";
        if (Justify == 'l')
            fmt = "%-" + len + "s";
        else
            fmt = "%" + len + "s";

        if (string.length() <= len) {
            String[] splits = string.split("\n");
            ArrayList<String> arr = new ArrayList<String>(splits.length);
            for (int i = 0; i < splits.length; i++)
                arr.add(String.format(fmt, splits[i]));

            return arr;
        }

        int chunks = string.length() / len + ((string.length() % len > 0) ? 1 : 0);
        ArrayList<String> arr = new ArrayList<String>(chunks);
        char[] carr = string.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < carr.length; i++) {
            if (sb.length() == len) {
                arr.add(sb.toString());
                sb = new StringBuilder();
                sb.append(carr[i]);
            } else if (carr[i] == '\n' && splitAtNewLine) {
                arr.add(String.format(fmt, sb.toString().trim()));
                sb = new StringBuilder();
            } else
                sb.append(carr[i]);
        }
        arr.add(String.format(fmt, sb.toString().trim()));
        return arr;
    }

    public static String[] splitByLength(String string, int len, int SplitCount) {
        if (string == null || len <= 0)
            return null;

        if (string.length() <= len)
            return new String[] { string };

        int chunks = string.length() / len + ((string.length() % len > 0) ? 1 : 0);
        chunks = chunks > SplitCount ? SplitCount : chunks;
        String[] arr = new String[chunks];
        int i = 0, j = 0;
        for (int l = string.length(); i < l && j < SplitCount - 1; i += len, j++) {
            if ((i + len) > l)
                break;
            else
                arr[j] = string.substring(i, i + len);
        }
        arr[j] = string.substring(i);
        return arr;
    }

    public static String Trim(String argInStr) {
        StringBuilder RegEx = new StringBuilder(argInStr);
        int i = 0;
        // Replace tab with space
        i = 0;
        while ((i = RegEx.indexOf("\t", i)) >= 0) {
            RegEx.replace(i, i + 1, "");
        }
        // Replace LineFeed with null
        i = 0;
        while ((i = RegEx.indexOf("\r", i)) >= 0) {
            RegEx.replace(i, i + 1, "");
        }
        // Replace CarriageReturn with space
        i = 0;
        while ((i = RegEx.indexOf("\n", i)) >= 0) {
            RegEx.replace(i, i + 1, " ");
        }
        // Replace double spaces with space
        i = 0;
        while ((i = RegEx.indexOf("  ", i)) >= 0) {
            RegEx.replace(i, i + 2, " ");
        }

        return RegEx.toString().trim();
    }
}

Related

  1. getSplitLines(String input, int maxLineLength)
  2. lineSplit(String text, int len)
  3. split(final String value, final int maxLength)
  4. split(String message, int maxPages, int pageSize)
  5. splitArray(byte[] array, int len)
  6. splitByteArray(byte[] inByteArray, int length)
  7. splitByteArray(final byte[] data, final int packetLength)
  8. splitByTrimmedDelimiterNonNestedInBracketsOrQuotesOrComments(final String s, final char delimiter, final char quoteChar, char bracketChar, final boolean includeIntermediaryEmptyElmts, final int maxElmts, String cmtStart, String cmtEnd)
  9. splitByWholeSeparatorWorker(final String str, final String separator, final int max, final boolean preserveAllTokens)