Java Utililty Methods String Split by Delimiter

List of utility methods to do String Split by Delimiter

Description

The list of methods to do String Split by Delimiter are organized into topic(s).

Method

String[]split(String input, String delimiter)
Split input by finding all occurrences of 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()]);
Listsplit(String input, String delimiter)
Method split.
if (input == null)
    return null;
List<String> splitList = new ArrayList<String>(16);
if (input.length() == 0)
    return splitList;
int startIndex = 0;
int endIndex;
do {
...
Listsplit(String input, String... delimiters)
splits the string based on the given delimiters.
if (input == null)
    return null;
String str = input;
List<String> list = new ArrayList<String>();
String delim = delimiters[0];
for (int i = 1; i < delimiters.length; i++) {
    str = str.replaceAll(delimiters[i], delim);
String[] tmp = str.split(delim);
for (int i = 0; i < tmp.length; i++) {
    list.add(tmp[i].trim());
return list;
Listsplit(String input, String... delimiters)
If no delimiters are supplied, one-element list containing input
if (input == null)
    return null;
String str = input;
List<String> list = new ArrayList<String>();
if (delimiters == null || delimiters.length < 1) {
    list.add(input);
    return list;
String delim = delimiters[0];
for (int i = 1; i < delimiters.length; i++) {
    str = str.replaceAll(delimiters[i], delim);
String[] tmp = str.split(delim);
for (int i = 0; i < tmp.length; i++) {
    list.add(tmp[i].trim());
return list;
String[]split(String inputStr, String delimeter, String enclosureStr)
Custom String split method, which handle the splitting of string containing a delimeter and enclosure

Use case #1: where delimeter character doesn't appear within the pair(s) of enclosure characters in input data

Example: inputData = "'20003', 'johndoe111@yahoo.com', 'John Doe', 'user', '0'";

Use case #2: where delimeter DOES appear within the pair(s) of enclosure

Example: inputData = "'20003', 'johndoe111@yahoo.com', 'John,, ,Doe', 'user', '0'";

Use case #3: where no enclosure pairs are specified in the input data

Example: inputData = "20003, johndoe111@yahoo.com, John Doe, user, 0";

The maximum splitting limit is imposed and set at 1000 internally.

if (inputStr == null || inputStr.isEmpty()) {
    return new String[0]; 
if (delimeter == null || delimeter.isEmpty()) {
    String[] strArr = new String[1];
    strArr[0] = inputStr;
    return strArr;
String enclosure = (enclosureStr != null) ? enclosureStr : "";
String[] strArr = inputStr.split(delimeter, MAX_LIMIT);
StringBuilder temp = new StringBuilder();
boolean notProperlyEnclosed = false;
int resetAtIndex = -1;
for (int i = 0; i < strArr.length; i++) {
    if (!enclosure.isEmpty()) {
        temp.append(strArr[i]);
    strArr[i] = strArr[i].trim(); 
    if (strArr[i].startsWith(enclosure) && strArr[i].endsWith(enclosure)) { 
        strArr[i] = strArr[i].substring(enclosure.length(), strArr[i].length() - enclosure.length());
        temp.delete(0, temp.length());
    } else { 
        notProperlyEnclosed = true;
        if (strArr[i].startsWith(enclosure)) {
            temp.append(delimeter);
            resetAtIndex = i;
        } else if (strArr[i].endsWith(enclosure)) {
            strArr[resetAtIndex] = temp.toString();
            strArr[resetAtIndex] = strArr[resetAtIndex].trim();
            strArr[resetAtIndex] = strArr[resetAtIndex].substring(enclosure.length(),
                    strArr[resetAtIndex].length() - enclosure.length());
            temp.delete(0, temp.length()); 
            strArr[i] = "{{**marked-as-dont-care**}}";
            resetAtIndex = -1; 
        } else {
            temp.append(delimeter);
            strArr[i] = "{{**marked-as-dont-care**}}";
if (notProperlyEnclosed) {
    List<String> newStrArr = new ArrayList<String>();
    for (int i = 0; i < strArr.length; i++) {
        if (strArr[i] != null && !strArr[i].equals("{{**marked-as-dont-care**}}")) {
            newStrArr.add(strArr[i]);
    String str[] = (String[]) newStrArr.toArray(new String[newStrArr.size()]);
    return str;
return strArr;
ArrayListsplit(String s, char delim)
split
ArrayList<String> rv = new ArrayList<String>();
int length = s.length();
int cur = -1;
int next;
if (length == 0) {
    return rv;
while (true) {
...
String[]split(String s, char delim)
split
if (isEmpty(s)) {
    return new String[0];
List list = new ArrayList();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    if (c == delim) {
...
Listsplit(String s, char delimiter)
Simple split method that preserves empty-string tokens.
List<String> tokens = new ArrayList<String>();
for (int start = 0, index = 0;; start = index + 1) {
    index = s.indexOf(delimiter, start);
    if (index != -1) {
        tokens.add(s.substring(start, index));
    } else {
        tokens.add(s.substring(start));
        break;
...
String[]split(String s, char delimiter)
split
s = s.trim();
if (s.isEmpty()) {
    return _emptyStringArray;
List<String> values = new ArrayList<>();
int offset = 0;
int pos = s.indexOf(delimiter, offset);
while (pos != -1) {
...
String[]split(String s, char delimiter)
Exactly like split(s, Character.toString(delimiter))
return split(s, Character.toString(delimiter));