Java String Quote quote(String s, String specials, char quoteChar)

Here you can find the source of quote(String s, String specials, char quoteChar)

Description

quote

License

Open Source License

Declaration

public static String quote(String s, String specials, char quoteChar) 

Method Source Code

//package com.java2s;
/*/*w  w w .  ja  v a  2s  .co m*/
 Copyright (C) 2003 Morten O. Alver
    
 All programs in this directory and
 subdirectories are published under the GNU General Public License as
 described below.
    
 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
    
 Further information about the GNU GPL is available at:
 http://www.gnu.org/copyleft/gpl.ja.html
    
 */

public class Main {
    public static String quote(String s, String specials, char quoteChar) {
        return quote(s, specials, quoteChar, 0);
    }

    /**
     * Quote special characters.
     * 
     * @param s
     *            The String which may contain special characters.
     * @param specials
     *            A String containing all special characters except the quoting
     *            character itself, which is automatically quoted.
     * @param quoteChar
     *            The quoting character.
     * @param linewrap
     *            The number of characters after which a linebreak is inserted
     *            (this linebreak is undone by unquote()). Set to 0 to disable.
     * @return A String with every special character (including the quoting
     *         character itself) quoted.
     */
    public static String quote(String s, String specials, char quoteChar, int linewrap) {
        StringBuffer sb = new StringBuffer();
        char c;
        int linelength = 0;
        boolean isSpecial;
        for (int i = 0; i < s.length(); ++i) {
            c = s.charAt(i);
            isSpecial = specials.indexOf(c) >= 0 || c == quoteChar;
            // linebreak?
            if (linewrap > 0 && (++linelength >= linewrap || (isSpecial && linelength >= linewrap - 1))) {
                sb.append(quoteChar);
                sb.append('\n');
                linelength = 0;
            }
            if (isSpecial) {
                sb.append(quoteChar);
                ++linelength;
            }
            sb.append(c);
        }
        return sb.toString();
    }
}

Related

  1. quote(String s)
  2. quote(String s)
  3. quote(String s)
  4. quote(String s, char ch)
  5. quote(String s, String nullResult)
  6. quote(String str)
  7. quote(String str)
  8. quote(String str)
  9. quote(String str)