Split String to List with specified separator - Android java.lang

Android examples for java.lang:String Split

Description

Split String to List with specified separator

Demo Code

import java.io.UnsupportedEncodingException;
import java.util.*;

public class Main{

    /**/*ww w. j a va  2  s .  c  o m*/
     * Split String to List
     *
     * @param strInput  String with splitor
     * @param separator Splitor, such as ',' '|'
     * @return String Item in List
     */
    public static List<String> splitString(String strInput, String separator) {
        List<String> listResult = new ArrayList<String>();
        if (strInput == null) {
            return null;
        }
        int start = 0;
        int end = strInput.length();
        while (start < end) {
            int separatorIndex = strInput.indexOf(separator, start);
            if (separatorIndex < 0) {
                String tok = strInput.substring(start);
                listResult.add(tok.trim());
                start = end;
            } else {
                String tok = strInput.substring(start, separatorIndex);
                listResult.add(tok.trim());
                start = separatorIndex + separator.length();
            }
        }
        return listResult;
    }

}

Related Tutorials