Java String Quote quote4CMDLine(String str)

Here you can find the source of quote4CMDLine(String str)

Description

quote CMD Line

License

Apache License

Declaration

private static String quote4CMDLine(String str) 

Method Source Code

//package com.java2s;
/*/*  ww  w  .  j a v  a  2 s . co m*/
 * Copyright 2013 Keith D Swenson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors Include: Shamim Quader, Sameer Pradhan, Kumar Raja, Jim Farris,
 * Sandia Yang, CY Chen, Rajiv Onat, Neal Wang, Dennis Tam, Shikha Srivastava,
 * Anamika Chaudhari, Ajay Kakkar, Rajeev Rastogi
 */

public class Main {
    private static String quote4CMDLine(String str) {
        //passing a null in results a no output, no quotes, nothing
        if (str == null) {
            return "\"\"";
        }
        int len = str.length();
        int startPos = 0;
        String trans = null;

        StringBuffer res = new StringBuffer("\"");
        for (int i = 0; i < len; i++) {
            char ch = str.charAt(i);
            switch (ch) {
            case '\"':
                trans = "\\\"";
                break;
            default:
                continue;
            }
            if (trans != null) {
                if (i > startPos) {
                    res.append(str.substring(startPos, i));
                }
                res.append(trans);
                startPos = i + 1;
                trans = null;
            }
        }
        // now write out whatever is left
        if (len > startPos) {
            res.append(str.substring(startPos));
        }
        res.append("\"");
        return res.toString();
    }
}

Related

  1. quote(String value)
  2. quote(String value)
  3. quote(String value)
  4. quote(StringBuilder buf, String str)
  5. quote(StringBuilder sb, String str)
  6. quoteAndClean(String str)
  7. quoteAndEscape(String str)
  8. quoteAndEscapeFilename(String filename)
  9. quoteAndEscapeValue(String serviceQName)