Java String Split by Delimiter splitToList(String str, char delimiter)

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

Description

split To List

License

Open Source License

Declaration

public static List<String> splitToList(String str, char delimiter) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {

    public static List<String> splitToList(String str, char delimiter) {
        // return no groups if we have an empty string
        if (str == null || "".equals(str)) {
            return Collections.emptyList();
        }/*from  ww w.ja v  a  2  s  . c  om*/

        ArrayList<String> parts = new ArrayList<String>();
        int currentIndex;
        int previousIndex = 0;

        while ((currentIndex = str.indexOf(delimiter, previousIndex)) > 0) {
            String part = str.substring(previousIndex, currentIndex).trim();
            parts.add(part);
            previousIndex = currentIndex + 1;
        }

        parts.add(str.substring(previousIndex, str.length()).trim());

        return parts;
    }
}

Related

  1. splitToArray(String stringToSplit, String delimitter, boolean trim)
  2. splitToken(String str, String delimiter)
  3. splitToList(final String string, final String delim, final int limit)
  4. splitToList(String a_text, String a_delimiter)
  5. splitToList(String s, String delimRegEx)
  6. splitUpTo(String s, String delimiter, int cnt)
  7. splitWhileKeepingParentheses(String value, char delimiter)
  8. splitWithEscapeChar(String s, char delimiter, char escape)
  9. stringSplitter(String inStr, String delimiterStr)