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

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

Description

Mimics the the Java SE String#split(String) method.

License

LGPL

Parameter

Parameter Description
s the string to split.
separator the separator to use.

Return

the array of split strings.

Declaration

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

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.ArrayList;

public class Main {
    /**// ww  w.  j a  v a2s. com
     * Mimics the the Java SE {@link String#split(String)} method.
     *
     * @param s the string to split.
     * @param separator the separator to use.
     * @return the array of split strings.
     */
    public static String[] split(String s, String separator) {
        int separatorlen = separator.length();
        ArrayList tokenList = new ArrayList();
        String tmpString = "" + s;
        int pos = tmpString.indexOf(separator);
        while (pos >= 0) {
            String token = tmpString.substring(0, pos);
            tokenList.add(token);
            tmpString = tmpString.substring(pos + separatorlen);
            pos = tmpString.indexOf(separator);
        }
        if (tmpString.length() > 0)
            tokenList.add(tmpString);
        String[] res = new String[tokenList.size()];
        for (int i = 0; i < res.length; i++) {
            res[i] = (String) tokenList.get(i);
        }
        return res;
    }
}

Related

  1. split(String s, char separator)
  2. split(String s, char separator)
  3. split(String s, char separator)
  4. split(String s, char separator)
  5. split(String s, char separator)
  6. split(String s, String separator)
  7. split(String s, String separator)
  8. split(String s, String separator)
  9. split(String src, char separator)