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

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

Description

Split string to array.

License

Open Source License

Parameter

Parameter Description
str String to split.
separator Separator.

Return

Split values.

Declaration

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

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 {
    /**//  ww w. j  a  v a  2  s.  c om
     * 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 expression, final char separator)
  2. split(final String input, final String separator)
  3. split(final String iSource, final char iRecordSeparator, final char... iJumpCharacters)
  4. split(final String s, final char separator)
  5. split(final String str, final char separator)
  6. split(final String str, final char separatorChar)
  7. split(final String str, final char separatorChar)
  8. split(final String str, final char[] separators)
  9. split(final String text, final char separator)