Java String Unescape unescapeString(String t)

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

Description

unescape String

License

Open Source License

Declaration

public static String unescapeString(String t) 

Method Source Code

//package com.java2s;
/**/*w ww . j a va  2s . co m*/
 * Copyright 2004-2014 Riccardo Solmi. All rights reserved.
 * This file is part of the Whole Platform.
 *
 * The Whole Platform 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.
 *
 * The Whole Platform 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 the Whole Platform. If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static String unescapeString(String t) {
        StringBuilder r = new StringBuilder();
        int length = t.length();
        int j = 0;
        while (j < length) {
            char c = t.charAt(j);
            if (c == '\\') {
                c = t.charAt(++j);
                switch (c) {
                case 'n':
                    r.append('\n');
                    break;
                case 't':
                    r.append('\t');
                    break;
                case 'b':
                    r.append('\b');
                    break;
                case 'r':
                    r.append('\r');
                    break;
                case 'f':
                    r.append('\f');
                    break;
                case '\\':
                    r.append('\\');
                    break;
                case '\'':
                    r.append('\'');
                    break;
                case '"':
                    r.append('"');
                    break;
                case 'x':
                    r.append((char) Integer.parseInt(t.substring(j + 1, j + 3), 16));
                    j += 2;
                    break;
                case 'u':
                    r.append((char) Integer.parseInt(t.substring(j + 1, j + 5), 16));
                    j += 4;
                    break;
                default:
                    if (Character.isDigit(c)) {
                        r.append((char) Integer.parseInt(t.substring(j, j + 3), 8));
                        j += 2;
                    } else
                        throw new IllegalArgumentException("Bad escaped string: " + t);
                }
            } else
                r.append(c);
            j++;
        }
        return r.toString();
    }
}

Related

  1. unescapeString(String str)
  2. unEscapeString(String str)
  3. unEscapeString(String str)
  4. unescapeString(String string)
  5. unescapeString(String string, String escapeChars, char escapeSymbol)
  6. unescapeString(String text)
  7. unescapeString(String text)
  8. unescapeString(String text)
  9. unescapeString(String txt)