Java XML Parse String parseArray(String array)

Here you can find the source of parseArray(String array)

Description

Parse an SQL array.

License

Open Source License

Parameter

Parameter Description
array The array to parse.

Return

The parsed array, as a list.

Declaration

public static List<String> parseArray(String array) 

Method Source Code

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

import java.util.List;

public class Main {
    /**/*w w  w .  j  ava  2  s . co  m*/
     * Parse an SQL array.
     * @param array The array to parse.
     * @return The parsed array, as a list.
     */
    public static List<String> parseArray(String array) {
        char[] input = array.substring(1, array.length() - 1)
                .replace("\\\\", "\\").toCharArray();
        List<String> output = new ArrayList<>();
        StringBuilder elem = new StringBuilder();
        boolean inQuotes = false;
        boolean escaped = false;
        for (char c : input) {
            if (escaped) {
                elem.append(c);
                escaped = false;
            } else if (c == '"') {
                inQuotes = !inQuotes;
                escaped = false;
            } 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;
    }
}

Related

  1. parseArguments(String args)
  2. parseArguments(String[] arguments, String delimiter)
  3. parseArgumentString(String argStr)
  4. parseArgumentString(String command)
  5. parseArray(String array)
  6. parseArray(String inputArray)
  7. parseXml(Object obj)
  8. parseXML(String path)
  9. parseXML(String pathToMap)