Java String Split by Quote splitIgnoringQuotes(String str, char separatorChar)

Here you can find the source of splitIgnoringQuotes(String str, char separatorChar)

Description

split Ignoring Quotes

License

Open Source License

Declaration

public static String[] splitIgnoringQuotes(String str, char separatorChar) 

Method Source Code


//package com.java2s;
import java.util.ArrayList;

import java.util.List;

public class Main {
    private static final String[] EMPTY_STRING_ARRAY = new String[0];

    public static String[] splitIgnoringQuotes(String str, char separatorChar) {
        if (str == null) {
            return null;
        }/*from w ww. j a  va 2  s.c  o  m*/

        int len = str.length();

        if (len == 0) {
            return EMPTY_STRING_ARRAY;
        }

        List<String> list = new ArrayList<String>();
        int i = 0;
        int start = 0;
        boolean match = false;

        while (i < len) {
            if (str.charAt(i) == '"') {
                i++;
                while (i < len) {
                    if (str.charAt(i) == '"') {
                        i++;
                        break;
                    }
                    i++;
                }
                match = true;
                continue;
            }
            if (str.charAt(i) == separatorChar) {
                if (match) {
                    list.add(str.substring(start, i));
                    match = false;
                }
                start = ++i;
                continue;
            }
            match = true;
            i++;
        }
        if (match) {
            list.add(str.substring(start, i));
        }

        return list.toArray(new String[list.size()]);
    }
}

Related

  1. quoteAwareSplit(String str, char delim)
  2. split(String sString, boolean bIgnoreQuotes)
  3. split(String str, char chrSplit, char chrQuote)
  4. splitArrayStringQuoted(String array, char quoteChar)
  5. splitHandleQuotes(String s)
  6. splitIgoringQuotes(String str)
  7. splitOnCharWithQuoting(String s, char splitChar, char quoteChar, char escapeChar)
  8. splitOnCharWithQuoting(String s, char splitChar, char quoteChar, char escapeChar)
  9. splitOptions(String quotedOptionString)