Java String Quote quoteText(String textToQuote, boolean sbAppend)

Here you can find the source of quoteText(String textToQuote, boolean sbAppend)

Description

quote Text

License

Open Source License

Declaration

static String quoteText(String textToQuote, boolean sbAppend) 

Method Source Code

//package com.java2s;
/*//from w  w w  . jav a  2  s.  c o  m
 * Copyright (C) 2003 Gerd Wagner
 *
 * 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 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 {
    static String quoteText(String textToQuote, boolean sbAppend) {
        if (null == textToQuote) {
            throw new IllegalArgumentException("textToQuote can not be null");
        }

        String[] lines = textToQuote.split("\n");

        StringBuffer ret = new StringBuffer();

        if (sbAppend) {
            ret.append("sb.append(\"").append(trimRight(lines[0].replaceAll("\"", "\\\\\"")));
        } else {
            ret.append("\"").append(trimRight(lines[0].replaceAll("\"", "\\\\\"")));
        }

        for (int i = 1; i < lines.length; ++i) {
            if (sbAppend) {
                ret.append(" \"); \nsb.append(\"").append(trimRight(lines[i].replaceAll("\"", "\\\\\"")));
            } else {
                ret.append(" \" +\n\"").append(trimRight(lines[i].replaceAll("\"", "\\\\\"")));
            }
        }

        if (sbAppend) {
            ret.append(" \");");
        } else {
            ret.append(" \";");
        }

        return ret.toString();
    }

    static String trimRight(String toTrim) {
        if (0 >= toTrim.length()) {
            return toTrim;
        }

        int i;
        for (i = toTrim.length(); i > 0; --i) {
            if (!Character.isWhitespace(toTrim.charAt(i - 1))) {
                break;
            }
        }

        return toTrim.substring(0, i);
    }
}

Related

  1. quoteStringLiteral(String string)
  2. quoteStringSQL(String s)
  3. quoteStringSQL(String s)
  4. quoteStringValue(String value)
  5. quoteText(String text)
  6. quoteTo(CharSequence cs, StringBuilder target)
  7. quoteTokenize(String clientResponse)
  8. QuoteValue(String str)
  9. quoteValue(String value)