split string as the String.split() without using regexes, faster - Android java.lang

Android examples for java.lang:String Split

Description

split string as the String.split() without using regexes, faster

Demo Code

import android.text.TextUtils;
import java.util.ArrayList;
import java.util.List;

public class Main{

    /**/*w ww  .  ja v a2s .c  om*/
     * same as the String.split(), except it doesn't use regexes, so it's faster.
     *
     * @param str       - the string to split up
     * @param delimiter the delimiter
     * @return the split string
     */
    public static String[] split(String str, String delimiter) {
        List<String> result = new ArrayList<String>();
        int lastIndex = 0;
        int index = str.indexOf(delimiter);
        while (index != -1) {
            result.add(str.substring(lastIndex, index));
            lastIndex = index + delimiter.length();
            index = str.indexOf(delimiter, index + delimiter.length());
        }
        result.add(str.substring(lastIndex, str.length()));

        return ArrayUtil.toArray(result, String.class);
    }

}

Related Tutorials