Java String Quote quoteFileName(String filename)

Here you can find the source of quoteFileName(String filename)

Description

Quotes a file name if it contains whitespaces and has not already been quoted.

License

Open Source License

Parameter

Parameter Description
filename the file to quote

Return

a quoted version of the specified filename

Declaration

public static String quoteFileName(String filename) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2001, 2007 Mathew A. Nelson and Robocode contributors
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://robocode.sourceforge.net/license/cpl-v10.html
 *
 * Contributors://from   w  w  w .  ja va  2s  .com
 *     Mathew A. Nelson
 *     - Initial API and implementation
 *     Flemming N. Larsen
 *     - Moved the former robocode.util.Constants and all file operations from
 *       robocode.util.Utils into this new class
 *     - Added/updated JavaDoc for all methods
 *     - Added the quoteFileName(), createDir(), getRobotsDir(), getConfigDir(),
 *       getRobocodeConfigFile(), getWindowConfigFile(), getCompilerConfigFile()
 *******************************************************************************/

public class Main {
    /**
     * Quotes a file name if it contains whitespaces and has not already been
     * quoted.
     *
     * @param filename the file to quote
     * @return a quoted version of the specified filename
     */
    public static String quoteFileName(String filename) {
        if (filename.startsWith("\"") || filename.endsWith("\"")) {
            return filename;
        }
        if (System.getProperty("os.name").toLowerCase().startsWith("windows") && filename.startsWith("file://")) {
            filename = filename.substring(7);
        }
        if (filename.matches(".*\\s+?.*")) {
            return '"' + filename + '"';
        }
        return filename;
    }
}

Related

  1. quotedStringOrNull(String string)
  2. quoteEach(String string)
  3. quoteEscape(final String original)
  4. quoteEscaped(String input)
  5. quoteExecutable(String executable)
  6. quoteForBatchScript(String arg)
  7. quoteForCommandString(String s)
  8. quoteForCsv(final String field)
  9. quoteForHTML(String toQuote)