Java String Split by Char split(String s, char c, int limit)

Here you can find the source of split(String s, char c, int limit)

Description

split

License

Open Source License

Declaration

public static String[] split(String s, char c, int limit) 

Method Source Code


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

import java.util.ArrayList;

public class Main {
    public static String[] split(String s, char c, int limit) {
        if (s == null)
            return null;
        ArrayList<Integer> pos = new ArrayList<Integer>();
        int i = -1;
        while ((i = s.indexOf((int) c, i + 1)) > 0) {
            pos.add(Integer.valueOf(i));
        }/* w w w. j  a v a2s .  c o  m*/
        int n = pos.size();
        int[] p = new int[n];
        i = -1;
        for (int x : pos) {
            p[++i] = x;
        }
        if ((limit == 0) || (limit > n)) {
            limit = n + 1;
        }
        String[] result = new String[limit];
        if (n > 0) {
            result[0] = s.substring(0, p[0]);
        } else {
            result[0] = s;
        }
        for (i = 1; i < limit - 1; ++i) {
            result[i] = s.substring(p[i - 1] + 1, p[i]);
        }
        if (limit > 1) {
            result[limit - 1] = s.substring(p[limit - 2] + 1);
        }
        return result;
    }

    /**
     * Splits a string in several parts (tokens) that are separated by
     * delimiter. Delimiter is <b>always</b> surrounded by two strings! If there
     * is no content between two delimiters, empty string will be returned for
     * that token. Therefore, the length of the returned array will always be:
     * #delimiters + 1.
     * <p>
     * Method is much, much faster then regexp <code>String.split()</code>, and
     * a bit faster then <code>StringTokenizer</code>.
     *
     * @param src
     *            string to split
     * @param delimiter
     *            split delimiter
     *
     * @return array of split strings
     */
    public static String[] split(String src, String delimiter) {
        int maxparts = (src.length() / delimiter.length()) + 2; // one more for
        // the last
        int[] positions = new int[maxparts];
        int dellen = delimiter.length();

        int i, j = 0;
        int count = 0;
        positions[0] = -dellen;
        while ((i = src.indexOf(delimiter, j)) != -1) {
            count++;
            positions[count] = i;
            j = i + dellen;
        }
        count++;
        positions[count] = src.length();

        String[] result = new String[count];

        for (i = 0; i < count; i++) {
            result[i] = src.substring(positions[i] + dellen, positions[i + 1]);
        }
        return result;
    }

    public static String subString(String str, int start, int end) {
        int strLen = str.length();
        start = start > strLen ? strLen : start;
        start = start > 0 ? start : 0;
        if (end == -1) {
            end = strLen;
        } else {
            end = end > strLen ? strLen : end;
        }
        return str.substring(start, end);
    }
}

Related

  1. split(final String string, final char... toSplit)
  2. split(String cs, char sep)
  3. split(String s, char c)
  4. split(String s, char c)
  5. split(String s, char c)
  6. split(String s, char c, int limit)
  7. split(String s, char ch)
  8. split(String s, char divider)
  9. split(String s, char divider, String[] output)