Java String Split by Separator split(String str, String separator)

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

Description

split

License

Open Source License

Declaration

public static String[] split(String str, String separator) 

Method Source Code


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

public class Main {
    public static String[] split(String str, String separator) {
        String[] returnValue;/*ww  w  . j a v a 2s .c  o  m*/
        int index = str.indexOf(separator);
        if (index == -1) {
            returnValue = new String[] { str };
        } else {
            List<String> strList = new ArrayList<String>();
            int oldIndex = 0;
            while (index != -1) {
                String subStr = str.substring(oldIndex, index);
                strList.add(subStr);
                oldIndex = index + separator.length();
                index = str.indexOf(separator, oldIndex);
            }
            if (oldIndex != str.length()) {
                strList.add(str.substring(oldIndex));
            }
            returnValue = strList.toArray(new String[strList.size()]);
        }

        return returnValue;

    }
}

Related

  1. split(String str, char separatorChar)
  2. split(String str, char separatorChar)
  3. split(String str, char separatorChar)
  4. split(String str, char separatorChar, boolean preserveAllTokens)
  5. split(String str, String separator)
  6. split(String str, String separator, boolean preserveEmptyToken)
  7. split(String string, char separator)
  8. split(String strToSplit, String strSeparator, int iLimit)
  9. split(String text, String separators, String brackets)