Java String Split by Delimiter split(String input, String delimiter)

Here you can find the source of split(String input, String delimiter)

Description

Split input by finding all occurrences of delimiter.

License

Open Source License

Parameter

Parameter Description
delimiter is not a regular expression, just a string.

Return

An array of strings that is always n + 1 long, where n is the number of times the string delimiter appears in input.

Declaration

public static String[] split(String input, String delimiter) 

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 ww  . java 2 s .c  om
     * Split input by finding all occurrences of delimiter.  Works like a
     * sane version of String.split, but with no regular expressions.
     *
     * @param delimiter is not a regular expression, just a string.
     *
     * @return An array of strings that is always n + 1 long, where n is the
     *         number of times the string delimiter appears in input.
     */
    public static String[] split(String input, String delimiter) {
        List<String> ret = new ArrayList<String>();

        int lastI = 0;
        int i = input.indexOf(delimiter);
        while (i != -1) {
            ret.add(input.substring(lastI, i));
            lastI = i + delimiter.length();
            i = input.indexOf(delimiter, lastI);
        }

        ret.add(input.substring(lastI));

        return ret.toArray(new String[ret.size()]);
    }
}

Related

  1. split(final String src, final char delim)
  2. split(final String str, final char delim)
  3. split(final String str, final String delimiter)
  4. split(String a, String delim)
  5. split(String input, char delimiter)
  6. split(String input, String delimiter)
  7. split(String input, String... delimiters)
  8. split(String input, String... delimiters)
  9. split(String inputStr, String delimeter, String enclosureStr)