Java String Split by Delimiter SplitAt(String str, String delimiter)

Here you can find the source of SplitAt(String str, String delimiter)

Description

Splits a string by a delimiter.

License

Creative Commons License

Parameter

Parameter Description
str A string to split.
delimiter A string to signal where each substring begins and ends.

Exception

Parameter Description
IllegalArgumentException Delimiter is null or empty.
NullPointerException The parameter delimiter is null.

Return

An array containing strings that are split by the delimiter. If str is null or empty, returns an array whose sole element is the empty string.

Declaration

public static String[] SplitAt(String str, String delimiter) 

Method Source Code


//package com.java2s;
import java.util.*;

public class Main {
    /**/* ww  w  . j  ava2 s. co m*/
     * Splits a string by a delimiter. If the string ends with the delimiter, the
     * result will end with an empty string. If the string begins with the
     * delimiter, the result will start with an empty string.
     * @param str A string to split.
     * @param delimiter A string to signal where each substring begins and ends.
     * @return An array containing strings that are split by the delimiter. If str
     * is null or empty, returns an array whose sole element is the empty
     * string.
     * @throws IllegalArgumentException Delimiter is null or empty.
     * @throws NullPointerException The parameter {@code delimiter} is null.
     */
    public static String[] SplitAt(String str, String delimiter) {
        if (delimiter == null) {
            throw new NullPointerException("delimiter");
        }
        if (delimiter.length() == 0) {
            throw new IllegalArgumentException("delimiter is empty.");
        }
        if (((str) == null || (str).length() == 0)) {
            return new String[] { "" };
        }
        int index = 0;
        boolean first = true;
        ArrayList<String> strings = null;
        int delimLength = delimiter.length();
        while (true) {
            int index2 = str.indexOf(delimiter, index);
            if (index2 < 0) {
                if (first) {
                    String[] strret = new String[1];
                    strret[0] = str;
                    return strret;
                }
                strings = (strings == null) ? ((new ArrayList<String>())) : strings;
                strings.add(str.substring(index));
                break;
            } else {
                first = false;
                String newstr = str.substring(index, (index) + ((index2) - index));
                strings = (strings == null) ? ((new ArrayList<String>())) : strings;
                strings.add(newstr);
                index = index2 + delimLength;
            }
        }
        return strings.toArray(new String[] {});
    }
}

Related

  1. split(String[] srcArray, String delimiterRegExp)
  2. split2(String string, String delimiter)
  3. splitAll(String str, String delim)
  4. splitAndTrim(String string, String delim)
  5. SplitAt(String s, String delimiter)
  6. splitByStr(String s, String delim)
  7. splitChars(String str, String delimiters)
  8. splitFast(String text, char delim)
  9. splitForChar(final String string, final char delimiter)