Java String Unescape unescape(String str)

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

Description

unescape

License

Open Source License

Declaration

public static String unescape(String str) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2014 United States Government as represented by the
 * Administrator of the National Aeronautics and Space Administration.
 * All Rights Reserved.// ww w.j a  v  a2  s  .c o m
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

public class Main {
    /**
     * Escape characters.
     */
    public final static String EXCLAMATIONMARK = "_EXCLAMATIONMARK_";
    public final static String DQUOT = "_DQUOT_";
    public final static String SQUOT = "_SQUOT_";
    public final static String NUMBERSIGN = "_NUMBERSIGN_";
    public final static String DOLLARSIGN = "_DOLLARSIGN_";
    public final static String PERCENTAGE = "_PERCENTAGESIGN_";
    public final static String AMPERSAND = "_AMPERSAND_";
    public final static String APOSTROPHE = "_APOSTROPHE_";
    public final static String LPARENTHESI = "_LPARENTHESI_";
    public final static String RPARENTHESI = "_RPARENTHESI_";
    public final static String ASTERISK = "_ASTERISK_";
    public final static String PLUS = "_PLUS_";
    public final static String COMMA = "_COMMA_;";
    public final static String DOT = "_DOT_";
    public final static String SEMICOLON = "_SEMICOLON_";
    public final static String COLON = "_COLON_";
    public final static String QUESTIONMARK = "_QUESTIONMARK_";
    public final static String BACKSLASH = "_BSLASH_";
    public final static String FORWARDSLASH = "_FSLASH_";
    public final static String GREATERTHAN = "_GREATERTHAN_;";
    public final static String LESSTHAN = "_LESSTHAN_";
    public final static String EQUALSIGN = "_EQUALSIGN_";
    public final static String ATARROA = "_ATARROA_";
    public final static String LBRACKET = "_LBRACKET_";
    public final static String RBRACKET = "_RBRACKET_";
    public final static String CARET = "_CARET_";
    public final static String LCURLYBRACE = "_LCURLYBRACE_";
    public final static String RCURLYBRACE = "_RCURLYBRACE_";
    public final static String VERTICALBAR = "_VERTICALBAR_";
    public final static String TILDE = "_TILDE_";
    public final static String HYPHEN = "_HYPHEN_";
    public final static String SPACE = "_SPACE_";

    public static String unescape(String str) {
        if (str == null || str.length() == 0)
            return "";

        final StringBuilder result = new StringBuilder();
        int len = str.length();
        for (int i = 0; i < len; i++) {
            char character = str.charAt(i);
            switch (character) {
            case '_':
                if (str.startsWith(EXCLAMATIONMARK, i)) {
                    result.append('!');
                    i += (EXCLAMATIONMARK.length() - 2);
                } else if (str.startsWith(DQUOT, i)) {
                    result.append('\"');
                    i += (DQUOT.length() - 1);
                } else if (str.startsWith(SQUOT, i)) {
                    result.append('\'');
                    i += (SQUOT.length() - 1);
                } else if (str.startsWith(NUMBERSIGN, i)) {
                    result.append('#');
                    i += (NUMBERSIGN.length() - 1);
                } else if (str.startsWith(DOLLARSIGN, i)) {
                    result.append('$');
                    i += (DOLLARSIGN.length() - 1);
                } else if (str.startsWith(PERCENTAGE, i)) {
                    result.append('%');
                    i += (PERCENTAGE.length() - 1);
                } else if (str.startsWith(AMPERSAND, i)) {
                    result.append('&');
                    i += (AMPERSAND.length() - 1);
                } else if (str.startsWith(APOSTROPHE, i)) {
                    result.append('`');
                    i += (APOSTROPHE.length() - 1);
                } else if (str.startsWith(LPARENTHESI, i)) {
                    result.append('(');
                    i += (LPARENTHESI.length() - 1);
                } else if (str.startsWith(RPARENTHESI, i)) {
                    result.append(')');
                    i += (RPARENTHESI.length() - 1);
                } else if (str.startsWith(ASTERISK, i)) {
                    result.append('*');
                    i += (ASTERISK.length() - 1);
                } else if (str.startsWith(PLUS, i)) {
                    result.append('+');
                    i += (PLUS.length() - 1);
                } else if (str.startsWith(COMMA, i)) {
                    result.append(',');
                    i += (COMMA.length() - 1);
                } else if (str.startsWith(DOT, i)) {
                    result.append('.');
                    i += (DOT.length() - 1);
                } else if (str.startsWith(SEMICOLON, i)) {
                    result.append(';');
                    i += (SEMICOLON.length() - 1);
                } else if (str.startsWith(COLON, i)) {
                    result.append(':');
                    i += (COLON.length() - 1);
                } else if (str.startsWith(QUESTIONMARK, i)) {
                    result.append('?');
                    i += (QUESTIONMARK.length() - 1);
                } else if (str.startsWith(BACKSLASH, i)) {
                    result.append('\\');
                    i += (BACKSLASH.length() - 1);
                } else if (str.startsWith(FORWARDSLASH, i)) {
                    result.append('/');
                    i += (FORWARDSLASH.length() - 1);
                } else if (str.startsWith(GREATERTHAN, i)) {
                    result.append('>');
                    i += (GREATERTHAN.length() - 1);
                } else if (str.startsWith(LESSTHAN, i)) {
                    result.append('<');
                    i += (LESSTHAN.length() - 1);
                } else if (str.startsWith(EQUALSIGN, i)) {
                    result.append('=');
                    i += (EQUALSIGN.length() - 1);
                } else if (str.startsWith(ATARROA, i)) {
                    result.append('@');
                    i += (ATARROA.length() - 1);
                } else if (str.startsWith(LBRACKET, i)) {
                    result.append('[');
                    i += (LBRACKET.length() - 1);
                } else if (str.startsWith(RBRACKET, i)) {
                    result.append(']');
                    i += (RBRACKET.length() - 1);
                } else if (str.startsWith(CARET, i)) {
                    result.append('^');
                    i += (CARET.length() - 1);
                } else if (str.startsWith(LCURLYBRACE, i)) {
                    result.append('{');
                    i += (LCURLYBRACE.length() - 1);
                } else if (str.startsWith(RCURLYBRACE, i)) {
                    result.append('}');
                    i += (RCURLYBRACE.length() - 1);
                } else if (str.startsWith(VERTICALBAR, i)) {
                    result.append('|');
                    i += (VERTICALBAR.length() - 1);
                } else if (str.startsWith(TILDE, i)) {
                    result.append('~');
                    i += (TILDE.length() - 1);
                } else if (str.startsWith(HYPHEN, i)) {
                    result.append('-');
                    i += (HYPHEN.length() - 1);
                } else if (str.startsWith(SPACE, i)) {
                    result.append(' ');
                    i += (SPACE.length() - 1);
                } else {
                    result.append(character);
                }
                break;
            default:
                result.append(character);
                break;
            }
        }
        return result.toString().intern(); // intern so '==' works
    }
}

Related

  1. unescape(String src)
  2. unescape(String src)
  3. unescape(String src)
  4. unescape(String src)
  5. unescape(String st)
  6. unescape(String str)
  7. unescape(String str)
  8. unescape(String str)
  9. unescape(String str)