Java String Quote quoteIt(String orig)

Here you can find the source of quoteIt(String orig)

Description

Quote it.

License

Open Source License

Parameter

Parameter Description
orig the orig

Return

the string

Declaration

public static String quoteIt(String orig) 

Method Source Code

//package com.java2s;
/*/*ww w.  j  a  va2  s .  c om*/
Copyright (C) 2004  Federico Abascal
    
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

public class Main {
    /**
     * Quote it.
     * 
     * @param orig the orig
     * 
     * @return the string
     */
    public static String quoteIt(String orig) {
        //quote spaces (or other characters) of filenames:
        String result = replace(orig, " ", "\\ ");
        //orig = replace(orig, "\\\\ ", "\\ ");
        return result;
    }

    /**
     * Replace.
     * 
     * @param orig the orig
     * @param from the from
     * @param to the to
     * 
     * @return the string
     */
    public static String replace(String orig, String from, String to) {
        int fromLength = from.length();

        if (fromLength == 0)
            throw new IllegalArgumentException("String to be replaced must not be empty");

        int start = orig.indexOf(from);
        if (start == -1)
            return orig;

        boolean greaterLength = (to.length() >= fromLength);

        StringBuffer buffer;
        // If the "to" parameter is longer than (or
        // as long as) "from", the final length will
        // be at least as large
        if (greaterLength) {
            if (from.equals(to))
                return orig;
            buffer = new StringBuffer(orig.length());
        } else {
            buffer = new StringBuffer();
        }

        char[] origChars = orig.toCharArray();

        int copyFrom = 0;
        while (start != -1) {
            buffer.append(origChars, copyFrom, start - copyFrom);
            buffer.append(to);
            copyFrom = start + fromLength;
            start = orig.indexOf(from, copyFrom);
        }
        buffer.append(origChars, copyFrom, origChars.length - copyFrom);

        return buffer.toString();
    }
}

Related

  1. quoteIfNotNull(StringBuilder sb, String val)
  2. quoteIfString(Object obj)
  3. quoteIfString(Object obj)
  4. quoteIfString(Object... arguments)
  5. quoteImpl(String s, char delim)
  6. quoteJava(String s)
  7. quoteJavascriptString(String s)
  8. quoteJavaString(String s)
  9. quoteJavaString(String s)