Android String Split split(String str, char delimiter)

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

Description

split

Declaration

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

Method Source Code

//package com.java2s;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.text.TextUtils;

public class Main {
    public static List<String> split(String str, char delimiter) {

        if (TextUtils.isEmpty(str)) {
            return Collections.emptyList();
        }//from w w  w .j  a v  a2 s  . c o  m

        int substringsCount = 1;
        for (int i = 0, len = str.length(); i < len; i++) {
            if (str.charAt(i) == delimiter) {
                substringsCount++;
            }
        }

        if (substringsCount == 1) {
            return Collections.singletonList(str);
        }

        List<String> split = new ArrayList<String>(substringsCount);

        int index = str.indexOf(delimiter);
        int lastIndex = 0;
        while (index != -1) {

            split.add(str.substring(lastIndex, index));

            lastIndex = index + 1;
            index = str.indexOf(delimiter, lastIndex);
        }

        split.add(str.substring(lastIndex, str.length())); // add the final string after the last delimiter

        return split;
    }
}

Related

  1. split(String str, String delims, boolean trimTokens)
  2. stringToListStr(String input)
  3. splitStringOnWhitespace(String stringToSplit)
  4. stringToListByLine(final String fileContent)
  5. split(String str, String separatorChars)
  6. split(String str, char separator)
  7. parseDelimitedList(String list, char delimiter)
  8. splitIt(String s)
  9. stringToList(String str, String delimiter)