Java XML Parse String parseArguments(String[] arguments, String delimiter)

Here you can find the source of parseArguments(String[] arguments, String delimiter)

Description

Parses an array of string arguments so that things wrapped in quotes count as one element in the array.

License

Open Source License

Parameter

Parameter Description
arguments the arguments to sanitize
delimiter the delimiter to insert in between the elements in quotes (ie: " ")

Return

the sanitized array of string arguments

Declaration

public static String[] parseArguments(String[] arguments, String delimiter) 

Method Source Code


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

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

public class Main {
    /**/*from w  ww  .ja  va  2  s  . c  o  m*/
     * Parses an array of string arguments so that things wrapped in quotes count as one element in the array.
     *
     * @param arguments the arguments to sanitize
     * @param delimiter the delimiter to insert in between the elements in quotes (ie: " ")
     * @return the sanitized array of string arguments
     */
    public static String[] parseArguments(String[] arguments, String delimiter) {
        List<String> sanitized = new ArrayList<>();

        for (int i = 0; i < arguments.length; i++) {
            if (arguments[i].startsWith("\"")) {
                StringBuilder buffer = new StringBuilder();
                buffer.append(arguments[i].substring("\"".length()));
                buffer.append(delimiter);

                for (i++; i < arguments.length; i++) {
                    if (arguments[i].endsWith("\"")) {
                        buffer.append(arguments[i].substring(0, arguments[i].length() - "\"".length()));
                        buffer.append(delimiter);
                        break;
                    } else {
                        buffer.append(arguments[i]);
                        buffer.append(delimiter);
                    }
                }

                sanitized.add(buffer.toString());
            } else {
                sanitized.add(arguments[i]);
            }
        }

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

Related

  1. parseArgs(String[] args)
  2. parseArgsString(String args)
  3. parseArgsToVmArgs(String[] args, String prefixOfVmArgs)
  4. parseArguments(final String command)
  5. parseArguments(String args)
  6. parseArgumentString(String argStr)
  7. parseArgumentString(String command)
  8. parseArray(String array)
  9. parseArray(String array)