Java String Split by Delimiter splitToList(final String string, final String delim, final int limit)

Here you can find the source of splitToList(final String string, final String delim, final int limit)

Description

split To List

License

Apache License

Declaration

public static List<String> splitToList(final String string, final String delim, final int limit) 

Method Source Code

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

import java.util.ArrayList;

import java.util.List;

public class Main {
    /** An empty string constant */
    public static final String EMPTY = "";

    public static List<String> splitToList(final String string, final String delim, final int limit) {
        // get the count of delim in string, if count is > limit
        // then use limit for count. The number of delimiters is less by one
        // than the number of elements, so add one to count.
        int count = count(string, delim) + 1;
        if (limit > 0 && count > limit) {
            count = limit;/*  w  w  w  .  j  a  v a  2s. c  om*/
        }

        final List<String> strings = new ArrayList<String>(count);
        int begin = 0;

        for (int i = 0; i < count; i++) {
            // get the next index of delim
            int end = string.indexOf(delim, begin);

            // if the end index is -1 or if this is the last element
            // then use the string's length for the end index
            if (end == -1 || i + 1 == count) {
                end = string.length();
            }

            // if end is 0, then the first element is empty
            if (end == 0) {
                strings.add(EMPTY);
            } else {
                strings.add(string.substring(begin, end));
            }
            // update the begining index
            begin = end + 1;
        }
        return strings;
    }

    public static List<String> splitToList(final String string, final String delim) {
        return splitToList(string, delim, -1);
    }

    /**
     * Count the number of instances of substring within a string.
     * 
     * @param string
     *          String to look for substring in.
     * @param substring
     *          Sub-string to look for.
     * @return Count of substrings in string.
     */
    public static int count(final String string, final String substring) {
        int count = 0;
        int idx = 0;

        while ((idx = string.indexOf(substring, idx)) != -1) {
            idx++;
            count++;
        }

        return count;
    }

    /**
     * Count the number of instances of character within a string.
     * 
     * @param string
     *          String to look for substring in.
     * @param c
     *          Character to look for.
     * @return Count of substrings in string.
     */
    public static int count(final String string, final char c) {
        return count(string, String.valueOf(c));
    }
}

Related

  1. splitStringList(String strList, String delimit)
  2. splitStringUsingDelimiter(String name, String delimiter)
  3. splitText(String text, String delimiter)
  4. splitToArray(String stringToSplit, String delimitter, boolean trim)
  5. splitToken(String str, String delimiter)
  6. splitToList(String a_text, String a_delimiter)
  7. splitToList(String s, String delimRegEx)
  8. splitToList(String str, char delimiter)
  9. splitUpTo(String s, String delimiter, int cnt)