Java Array Concatenate concatenate(String[] args)

Here you can find the source of concatenate(String[] args)

Description

Returns the list of arguments as space separated string for the command line.

License

Apache License

Parameter

Parameter Description
args the arguments

Return

a comma separated string

Declaration

public static String concatenate(String[] args) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;

public class Main {
    /**/*from  w  w w  . j a v  a2s  .c om*/
     * Returns the list of arguments as space separated string for the command
     * line.
     *
     * @param args the arguments
     * 
     * @return a comma separated string
     */
    public static String concatenate(ArrayList<String> args) {

        if (args == null) {
            return null;
        }

        String quote = getQuoteType();
        String result = "";

        for (String arg : args) {
            if (!result.equals("")) {
                result += " ";
            }

            // add quotes around the arguments in order to support file names with spaces
            if (!arg.startsWith("-") && !arg.startsWith("\"")
                    && !arg.startsWith("\'")) {
                arg = quote + arg + quote;
            }

            result += arg;
        }

        return result;
    }

    /**
     * Returns the list of arguments as space separated string for the command
     * line. Adds quotes where they seem to be needed.
     *
     * @param args the arguments
     * 
     * @return a comma separated string
     */
    public static String concatenate(String[] args) {

        if (args == null) {
            return null;
        }

        String quote = getQuoteType();
        String result = "";

        for (String arg : args) {
            if (!result.equals("")) {
                result += " ";
            }

            // add quotes around the arguments in order to support file names with spaces
            if (!arg.startsWith("-") && !arg.startsWith("\"")
                    && !arg.startsWith("\'")) {
                arg = quote + arg + quote;
            }

            result += arg;
        }

        return result;
    }

    /**
     * Returns the quote type to use. For example around file paths with spaces.
     *
     * @return the quote type to use
     */
    public static String getQuoteType() {

        String quote = "";

        if (System.getProperty("os.name").lastIndexOf("Windows") != -1) {
            quote = "\"";
        }

        return quote;
    }
}

Related

  1. concatenate(Object[] array)
  2. concatenate(Object[] array)
  3. concatenate(Object[] array)
  4. concatenate(Object[] array)
  5. concatenate(Object[] collection)
  6. concatenate(String[] strings, String sep)
  7. concatenate(T[] first, T[] second)
  8. concatenate2Arrays(T[] array1, T... array2)
  9. concatenateArray(Object[] srcOne, Object[] srcTwo)