Java String Split split(String str)

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

Description

Split a string using the default separator

License

Open Source License

Parameter

Parameter Description
str a string that may have escaped separator

Return

an array of strings

Declaration

public static String[] split(String str) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;

public class Main {
    final public static char COMMA = ',';
    final public static char ESCAPE_CHAR = '\\';

    /**//  ww  w.  j a  v  a  2  s.  c o  m
     * Split a string using the default separator
     * 
     * @param str
     *            a string that may have escaped separator
     * @return an array of strings
     */
    public static String[] split(String str) {
        return split(str, ESCAPE_CHAR, COMMA);
    }

    /**
     * Split a string using the given separator
     * 
     * @param str
     *            a string that may have escaped separator
     * @param escapeChar
     *            a char that be used to escape the separator
     * @param separator
     *            a separator char
     * @return an array of strings
     */
    public static String[] split(String str, char escapeChar, char separator) {
        if (str == null) {
            return null;
        }
        ArrayList<String> strList = new ArrayList<String>();
        StringBuilder split = new StringBuilder();
        int index = 0;
        while ((index = findNext(str, separator, escapeChar, index, split)) >= 0) {
            ++index; // move over the separator for next search
            strList.add(split.toString());
            split.setLength(0); // reset the buffer
        }
        strList.add(split.toString());
        // remove trailing empty split(s)
        int last = strList.size(); // last split
        while (--last >= 0 && "".equals(strList.get(last))) {
            strList.remove(last);
        }
        return strList.toArray(new String[strList.size()]);
    }

    /**
     * Finds the first occurrence of the separator character ignoring the escaped separators starting from the index. Note the substring
     * between the index and the position of the separator is passed.
     * 
     * @param str
     *            the source string
     * @param separator
     *            the character to find
     * @param escapeChar
     *            character used to escape
     * @param start
     *            from where to search
     * @param split
     *            used to pass back the extracted string
     */
    public static int findNext(String str, char separator, char escapeChar, int start, StringBuilder split) {
        int numPreEscapes = 0;
        for (int i = start; i < str.length(); i++) {
            char curChar = str.charAt(i);
            if (numPreEscapes == 0 && curChar == separator) { // separator
                return i;
            } else {
                split.append(curChar);
                numPreEscapes = (curChar == escapeChar) ? (++numPreEscapes) % 2 : 0;
            }
        }
        return -1;
    }

    public static String toString(String[] content, String sign) {
        if (null == content) {
            return null;
        }

        sign = null == sign ? "," : sign;
        StringBuilder strBuilder = new StringBuilder();
        for (int i = 0; i < content.length; i++) {
            strBuilder.append(content[i]);
            if (i < content.length - 1) {
                strBuilder.append(sign);
            }
        }

        return strBuilder.toString();
    }
}

Related

  1. split(String str)
  2. split(String str)
  3. split(String str)
  4. split(String str)
  5. split(String str)
  6. split(String str)
  7. split(String str, int ch)
  8. split(String str, String id)
  9. split(String str, String splitStr)