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

Apache License

Parameter

Parameter Description
s The String to split
splitChar a parameter
quoteChar a parameter

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;
/**//from w  ww  . j a  va 2s.  c o m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 {
    /**
     * This function splits the String s into multiple Strings using the
     * splitChar.  However, it provides an 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
     * @param quoteChar
     * @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<>();
        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);
                    if (curr == escapeChar) {
                        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[0]);
    }
}

Related

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