Java String Split by Quote splitArrayStringQuoted(String array, char quoteChar)

Here you can find the source of splitArrayStringQuoted(String array, char quoteChar)

Description

Takes a string and splits it according to the quote character '.
Use this method for splitting a string into several parts, where each part corresponds to a quoted substring in the input.
A string "['stringA' 'stringB']" will result in the list (stringA,stringB) with ' being the character used for qouting.

License

Open Source License

Parameter

Parameter Description
array Input string, containing quoted character sets.
quoteChar The character used to quote strings in the input string.

Return

list, containing all quotes strings in the input string.

Declaration

public static List<String> splitArrayStringQuoted(String array, char quoteChar) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**//w ww  . j  a  v  a  2s  .  co  m
     * Takes a string and splits it according to the quote character '.<br>
     * Use this method for splitting a string into several parts, where each part corresponds to a quoted substring in the input.<br>
     * A string "['stringA' 'stringB']" will result in the list (stringA,stringB) with ' being the character used for qouting.
     * @param array Input string, containing quoted character sets.
     * @param quoteChar The character used to quote strings in the input string.
     * @return  list, containing all quotes strings in the input string.
     */
    public static List<String> splitArrayStringQuoted(String array, char quoteChar) {
        array = array.replace("[", "");
        array = array.replace("]", "");
        List<String> result = new ArrayList<>();
        Integer actStart = null;
        for (int i = 0; i < array.length(); i++) {
            if (array.charAt(i) == quoteChar) {
                if (actStart == null) {
                    if (i < array.length() - 1) {
                        actStart = i + 1;
                    }
                } else {
                    result.add(array.substring(actStart, i));
                    actStart = null;
                }
            }
        }
        return result;
    }

    /**
     * Takes a string and splits it according to the quote character.<br>
     * Use this method for splitting a string into several parts, where each part corresponds to a quoted substring in the input.<br>
     * A string "['stringA' 'stringB']" will result in the list (stringA,stringB) with ' being the character used for qouting.
     * @param array Input string, containing quoted character sets.
     * @return  list, containing all quotes strings in the input string.
     */
    public static List<String> splitArrayStringQuoted(String array) {
        return splitArrayStringQuoted(array, '\'');
    }
}

Related

  1. quoteAwareSplit(String str, char delim)
  2. split(String sString, boolean bIgnoreQuotes)
  3. split(String str, char chrSplit, char chrQuote)
  4. splitHandleQuotes(String s)
  5. splitIgnoringQuotes(String str, char separatorChar)
  6. splitIgoringQuotes(String str)
  7. splitOnCharWithQuoting(String s, char splitChar, char quoteChar, char escapeChar)