delimited List To String Array - Android java.lang

Android examples for java.lang:String Split

Description

delimited List To String Array

Demo Code

import android.text.Html;
import android.text.Spanned;
import java.util.ArrayList;
import java.util.List;

public class Main{

    public static String[] delimitedListToStringArray(String str,
            String delimiter) {//from  w  w w. j av  a  2s  .c o  m
        if (str == null) {
            return new String[0];
        }
        if (delimiter == null) {
            return new String[] { str };
        }

        List<String> result = new ArrayList<String>();
        if ("".equals(delimiter)) {
            result.add(str);
        } else {
            int pos = 0;
            int delPos = 0;
            while ((delPos = str.indexOf(delimiter, pos)) != -1) {
                result.add(str.substring(pos, delPos));
                pos = delPos + delimiter.length();
            }
            if (str.length() > 0 && pos <= str.length()) {
                // Add rest of String, but not in case of empty input.
                result.add(str.substring(pos));
            }
        }
        return result.toArray(new String[result.size()]);
    }

}

Related Tutorials