Here you can find the source of flattenArguments(String[] arguments)
Parameter | Description |
---|---|
arguments | The array of strings to flatten. |
static public String flattenArguments(String[] arguments)
//package com.java2s; /******************************************************************************* * Copyright (c) 2000, 2005 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/*ww w .j a v a2 s. c o m*/ * IBM Corporation - initial API and implementation *******************************************************************************/ public class Main { /** * This method flattens an array of arguments to a string. * The method respects embedded whitespace and quotes. * <ul> * <li>Any argument with embedded whitespace will be flattened out * with enclosing quotes. For example, the single argument * <u>Hello World</u> * will be returned as * <u>"Hello World"</u>. * <li>Any argument with quotes will be flattened out with the * quotes escaped. For example, the single argument * <u>"Happy days"</u> * will be returned as * <u>\"Happy days\"</u>. * </ul> * @param arguments The array of strings to flatten. * @return the flattened string. */ static public String flattenArguments(String[] arguments) { StringBuffer buf = new StringBuffer(); for (int i = 0; i < arguments.length; i++) { // // Append a separator (except the first time). // if (i > 0) buf.append(' '); // // Look for whitespace. // boolean whitespace = false; char[] chars = arguments[i].toCharArray(); for (int j = 0; !whitespace && j < chars.length; j++) { if (Character.isWhitespace(chars[j])) { whitespace = true; } } // // Append the argument, quoted as necessary. // if (whitespace) buf.append('"'); for (int j = 0; j < chars.length; j++) { if (chars[j] == '"') buf.append('\\'); buf.append(chars[j]); } if (whitespace) buf.append('"'); } return buf.toString(); } }