Here you can find the source of parseArray(String array)
Parameter | Description |
---|---|
array | The array to parse. |
public static List<String> parseArray(String array)
//package com.java2s; import java.util.ArrayList; import java.util.List; public class Main { /**//from w w w.ja v a 2 s . c om * Parse an SQL array. * * This code allows fixing "doubly escaped" quotes, but would fail if you actually * wanted \\\\ to become two backslashes in a string (see the tests). * It also doesn't support single quote strings in SQL, since our output/tests write single quotes without * escaping or quoting, but, really you should for SQL, as I understand things.... * And it has special support for double double quoting an entire string ... but this disables having empty strings. * I think that output comes from incorrectly not undoubling quotes in a String at an earlier stage, and so it * should be fixed earlier. * * @param array The array to parse. * @return The parsed array, as a list. */ public static List<String> parseArray(String array) { // array = unescapeSQL(array); if (array.startsWith("{") && array.endsWith("}")) { array = array.substring(1, array.length() - 1); } array = array.replace("\\\\", "\\"); // The questionable code for "doubly escaped" things char[] input = array.toCharArray(); List<String> output = new ArrayList<>(); StringBuilder elem = new StringBuilder(); boolean inQuotes = false; boolean escaped = false; boolean doubledQuotes = false; char lastQuoteChar = '\0'; for (int i = 0; i < input.length; i++) { char c = input[i]; char next = (i == input.length - 1) ? '\0' : input[i + 1]; if (escaped) { elem.append(c); escaped = false; } else if (c == '"') { // to support single quote escaping add: || c == '\'' if (!inQuotes) { inQuotes = true; escaped = false; lastQuoteChar = c; if (next == c) { // supporting doubling of beginning quote, expect doubling of ending, disable support for internal doubling i++; doubledQuotes = true; } } else { if (c == lastQuoteChar) { if (next == lastQuoteChar && !doubledQuotes) { // doubled quote escaping escaped = true; } else { inQuotes = false; escaped = false; if (doubledQuotes) { i++; doubledQuotes = false; } } } else { // different quote char, just like literal elem.append(c); } } } else if (c == '\\') { escaped = true; } else { if (inQuotes) { elem.append(c); } else if (c == ',') { output.add(elem.toString()); elem.setLength(0); // This is basically .clear() } else { elem.append(c); } escaped = false; } } if (elem.length() > 0) { output.add(elem.toString()); } return output; } }