Java Char Array Escape charsToEscapes(String str)

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

Description

Escapes newlines, tabs, backslashes, quotes in the specified string.

License

Open Source License

Parameter

Parameter Description
str The string

Declaration

public static String charsToEscapes(String str) 

Method Source Code

//package com.java2s;
/*/*from  www. ja v a2s .co m*/
 * MiscUtilities.java - Various miscallaneous utility functions
 * :tabSize=8:indentSize=8:noTabs=false:
 * :folding=explicit:collapseFolds=1:
 *
 * Copyright (C) 1999, 2000, 2001 Slava Pestov
 * Portions copyright (C) 2000 Richard S. Hall
 * Portions copyright (C) 2001 Dirk Moebius
 *
 * 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 {
    /**
     * Escapes newlines, tabs, backslashes, quotes in the specified
     * string.
     * @param str The string
     * @since jEdit 2.3pre1
     */
    public static String charsToEscapes(String str) {
        return charsToEscapes(str, false);
    }

    /**
     * Escapes newlines, tabs, backslashes, quotes in the specified
     * string.
     * @param str The string
     * @param history jEdit history files require additional escaping
     * @since jEdit 2.7pre2
     */
    public static String charsToEscapes(String str, boolean history) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            switch (c) {
            case '\n':
                buf.append("\\n");
                break;
            case '\t':
                buf.append("\\t");
                break;
            case '[':
                if (history)
                    buf.append("\\[");
                else
                    buf.append(c);
                break;
            case ']':
                if (history)
                    buf.append("\\]");
                else
                    buf.append(c);
                break;
            case '"':
                if (history)
                    buf.append(c);
                else
                    buf.append("\\\"");
                break;
            case '\'':
                if (history)
                    buf.append(c);
                else
                    buf.append("\\\'");
                break;
            case '\\':
                buf.append("\\\\");
                break;
            default:
                buf.append(c);
                break;
            }
        }
        return buf.toString();
    }
}

Related

  1. charsToEscapes(String str)