Java String Unescape unescapeString(String input)

Here you can find the source of unescapeString(String input)

Description

Convert sequences of characters (into an input string) representing an escape sequence into the corresponding escape sequences

License

Open Source License

Parameter

Parameter Description
input the input string

Return

the converted string

Declaration

public static String unescapeString(String input) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009, 2016 GreenVulcano ESB Open Source Project.
 * All rights reserved.//from ww w.  ja  v  a  2 s .c  o m
 *
 * This file is part of GreenVulcano ESB.
 *
 * GreenVulcano ESB is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GreenVulcano ESB 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with GreenVulcano ESB. If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/

public class Main {
    /**
     * Convert sequences of characters (into an input string) representing an
     * escape sequence into the corresponding escape sequences
     * 
     * @param input
     *        the input string
     * @return the converted string
     */
    public static String unescapeString(String input) {
        StringBuilder buf = new StringBuilder("");
        int i = 0;
        while (i < input.length()) {
            char currChar = input.charAt(i);
            if ((currChar == '\\') && (i < (input.length() - 1))) {
                char toAdd = 0;
                char nextChar = input.charAt(++i);
                if (nextChar == '0') {
                    // End-of-string escape sequence
                    toAdd = '\0';
                    buf.append(toAdd);
                    i++;
                } else if (nextChar == 'b') {
                    // backspace escape sequence
                    toAdd = '\b';
                    buf.append(toAdd);
                    i++;
                } else if (nextChar == 't') {
                    // tab escape sequence
                    toAdd = '\t';
                    buf.append(toAdd);
                    i++;
                } else if (nextChar == 'n') {
                    // linefeed escape sequence
                    toAdd = '\n';
                    buf.append(toAdd);
                    i++;
                } else if (nextChar == 'f') {
                    // formfeed escape sequence
                    toAdd = '\f';
                    buf.append(toAdd);
                    i++;
                } else if (nextChar == 'r') {
                    // carriage return escape sequence
                    toAdd = '\r';
                    buf.append(toAdd);
                    i++;
                } else if (nextChar == '\"') {
                    // double quote escape sequence
                    toAdd = '\"';
                    buf.append(toAdd);
                    i++;
                } else if (nextChar == '\'') {
                    // single quote escape sequence
                    toAdd = '\'';
                    buf.append(toAdd);
                    i++;
                } else if (nextChar == '\\') {
                    // backslash escape sequence
                    String strToAdd = "\\\\";
                    buf.append(strToAdd);
                    i++;
                } else {
                    // It's not an escape sequence
                    buf.append(currChar).append(nextChar);
                    i++;
                }
            } else {
                buf.append(currChar);
                i++;
            }
        }
        return buf.toString();
    }
}

Related

  1. unescapeString(String escapedString)
  2. unescapeString(String iccProfileSrc)
  3. unescapeString(String in)
  4. unescapeString(String inp)
  5. unEscapeString(String input)
  6. unescapeString(String oldstr)
  7. unescapeString(String s)
  8. unescapeString(String s)
  9. unescapeString(String s)