Java String Split by Quote splitOnCharWithQuoting(String s, char splitChar, char quoteChar, char escapeChar)

Here you can find the source of splitOnCharWithQuoting(String s, char splitChar, char quoteChar, char escapeChar)

Description

This function splits the String s into multiple Strings using the splitChar.

License

Open Source License

Parameter

Parameter Description
s The String to split
splitChar The character to split on
quoteChar The character to quote items with
escapeChar The character to escape the quoteChar with

Return

An array of Strings that s is split into

Declaration

public static String[] splitOnCharWithQuoting(String s, char splitChar,
        char quoteChar, char escapeChar) 

Method Source Code

//package com.java2s;

import java.util.*;

public class Main {
    /**/*www .j a  v a2 s .  c om*/
     * This function splits the String s into multiple Strings using the
     * splitChar.  However, it provides a quoting facility: it is possible to
     * quote strings with the quoteChar.
     * If the quoteChar occurs within the quotedExpression, it must be prefaced
     * by the escapeChar
     *
     * @param s         The String to split
     * @param splitChar The character to split on
     * @param quoteChar The character to quote items with
     * @param escapeChar The character to escape the quoteChar with
     * @return An array of Strings that s is split into
     */
    public static String[] splitOnCharWithQuoting(String s, char splitChar,
            char quoteChar, char escapeChar) {
        List<String> result = new ArrayList<String>();
        int i = 0;
        int length = s.length();
        StringBuilder b = new StringBuilder();
        while (i < length) {
            char curr = s.charAt(i);
            if (curr == splitChar) {
                // add last buffer
                if (b.length() > 0) {
                    result.add(b.toString());
                    b = new StringBuilder();
                }
                i++;
            } else if (curr == quoteChar) {
                // find next instance of quoteChar
                i++;
                while (i < length) {
                    curr = s.charAt(i);
                    // mrsmith: changed this condition from
                    // if (curr == escapeChar) {
                    if ((curr == escapeChar) && (i + 1 < length)
                            && (s.charAt(i + 1) == quoteChar)) {
                        b.append(s.charAt(i + 1));
                        i += 2;
                    } else if (curr == quoteChar) {
                        i++;
                        break; // break this loop
                    } else {
                        b.append(s.charAt(i));
                        i++;
                    }
                }
            } else {
                b.append(curr);
                i++;
            }
        }
        if (b.length() > 0) {
            result.add(b.toString());
        }
        return result.toArray(new String[result.size()]);
    }
}

Related

  1. splitArrayStringQuoted(String array, char quoteChar)
  2. splitHandleQuotes(String s)
  3. splitIgnoringQuotes(String str, char separatorChar)
  4. splitIgoringQuotes(String str)
  5. splitOnCharWithQuoting(String s, char splitChar, char quoteChar, char escapeChar)
  6. splitOptions(String quotedOptionString)
  7. splitQuoted(String input, Character split)
  8. splitQuoted(String str, char delimiter)
  9. splitQuotedEscape(String src)