Java String Split by Separator split(final String str, final char[] separators)

Here you can find the source of split(final String str, final char[] separators)

Description

Split string to array.

License

Open Source License

Parameter

Parameter Description
str String to split.
separators Separators.

Return

Split values.

Declaration

public static List<String> split(final String str,
        final char[] separators) 

Method Source Code

//package com.java2s;
// and/or modify it under the terms of the GNU General Public License 

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**//from  w ww. jav  a2  s. co  m
     * Split string to array.
     * 
     * @param str
     *            String to split.
     * @param separators
     *            Separators.
     * @return Split values.
     */
    public static List<String> split(final String str,
            final char[] separators) {
        int lastPos = 0;
        List<String> arr = new ArrayList<String>();
        for (int pos = 0; pos != str.length(); ++pos) {
            char ch = str.charAt(pos);
            for (char sep : separators) {
                if (ch == sep) {
                    if (lastPos != pos) {
                        arr.add(str.substring(lastPos, pos));
                    }
                    lastPos = pos + 1;
                    break;
                }
            }
        }
        if (lastPos != str.length()) {
            arr.add(str.substring(lastPos, str.length()));
        }
        return arr;
    }

    /**
     * Split string to array.
     * 
     * @param str
     *            String to split.
     * @param separator
     *            Separator.
     * @return Split values.
     */
    public static List<String> split(final String str, final char separator) {
        List<String> arr = new ArrayList<String>();
        int pos = 0, lastPos = 0;
        while ((pos = str.indexOf(separator, lastPos)) != -1) {
            arr.add(str.substring(lastPos, pos));
            lastPos = pos + 1;
        }
        if (str.length() > lastPos) {
            arr.add(str.substring(lastPos));
        } else {
            arr.add("");
        }
        return arr;
    }

    /**
     * Split string to array.
     * 
     * @param str
     *            String to split.
     * @param separator
     *            Separator.
     * @return Split values.
     */
    public static List<String> split(final String str,
            final String separator) {
        List<String> arr = new ArrayList<String>();
        int pos = 0, lastPos = 0;
        while ((pos = str.indexOf(separator, lastPos)) != -1) {
            arr.add(str.substring(lastPos, pos));
            lastPos = pos + separator.length();
        }
        if (str.length() > lastPos) {
            arr.add(str.substring(lastPos));
        } else {
            arr.add("");
        }
        return arr;
    }
}

Related

  1. split(final String s, final char separator)
  2. split(final String str, final char separator)
  3. split(final String str, final char separator)
  4. split(final String str, final char separatorChar)
  5. split(final String str, final char separatorChar)
  6. split(final String text, final char separator)
  7. split(final String text, final String separator)
  8. split(final T string, final char separator)
  9. split(String array, String separators)