Java String Split by Quote split(String str, char chrSplit, char chrQuote)

Here you can find the source of split(String str, char chrSplit, char chrQuote)

Description

split divides a string into many strings based on a delimiter The main difference between this split and the one that comes with Java is this one will ignore delimiters that are within quoted fields

NOTE: Delimiter will be ignored once chrQuote is encountered.

License

Apache License

Parameter

Parameter Description
str The string you want to split
chrSplit character you want to split the string on
chrQuote character you want to be considered to be your quoting character

Return

String array containing results of splitting the supplied String

Declaration

public static String[] split(String str, char chrSplit, char chrQuote) 

Method Source Code

//package com.java2s;
/**/*from  w w w.j a  v  a 2s .c  o m*/
 * Flatworm - A Java Flat File Importer Copyright (C) 2004 James M. Turner Extended by James Lawrence 2005
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 * 
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * <code>split</code> divides a string into many strings based on a delimiter
     * The main difference between this split and the one that comes with Java is
     * this one will ignore delimiters that are within quoted fields
     * <p>
     * <b>NOTE:</b> Delimiter will be ignored once chrQuote is encountered.
     * Consideration will begin once matching chrQuote is encountered
     * </p>
     * 
     * @param str
     *          The string you want to split
     * @param chrSplit
     *          character you want to split the string on
     * @param chrQuote
     *          character you want to be considered to be your quoting character
     * @return String array containing results of splitting the supplied String
     */
    public static String[] split(String str, char chrSplit, char chrQuote) {
        List<String> tokens = new ArrayList<String>();
        String str1 = new String();
        boolean inQuote = false;

        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == chrSplit && !inQuote) {
                tokens.add(str1);
                str1 = new String();
            } else if (str.charAt(i) == chrQuote) {
                inQuote = (!inQuote);
            } else {
                str1 += str.charAt(i);
            }
        }

        tokens.add(str1);

        return tokens.toArray(new String[0]);
    }
}

Related

  1. quoteAwareSplit(String str, char delim)
  2. split(String sString, boolean bIgnoreQuotes)
  3. splitArrayStringQuoted(String array, char quoteChar)
  4. splitHandleQuotes(String s)
  5. splitIgnoringQuotes(String str, char separatorChar)
  6. splitIgoringQuotes(String str)