Java String Unescape unescapeString(String text)

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

Description

unescape String

License

Apache License

Declaration

public static String unescapeString(String text) 

Method Source Code

//package com.java2s;
/*//w ww  .  j a  v a  2  s . co m
 * Copyright 2016 Red Hat, Inc. and/or its affiliates.
 *
 * 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 {
    public static String unescapeString(String text) {
        if (text == null) {
            return null;
        }
        if (text.length() >= 2 && text.startsWith("\"") && text.endsWith("\"")) {
            // remove the quotes
            text = text.substring(1, text.length() - 1);
        }
        if (text.indexOf('\\') >= 0) {
            // might require un-escaping
            StringBuilder r = new StringBuilder();
            for (int i = 0; i < text.length(); i++) {
                char c = text.charAt(i);
                if (c == '\\') {
                    if (text.length() > i + 1) {
                        i++;
                        char cn = text.charAt(i);
                        switch (cn) {
                        case 'b':
                            r.append('\b');
                            break;
                        case 't':
                            r.append('\t');
                            break;
                        case 'n':
                            r.append('\n');
                            break;
                        case 'f':
                            r.append('\f');
                            break;
                        case 'r':
                            r.append('\r');
                            break;
                        case '"':
                            r.append('"');
                            break;
                        case '\'':
                            r.append('\'');
                            break;
                        case '\\':
                            r.append('\\');
                            break;
                        case 'u': {
                            if (text.length() >= i + 5) {
                                // escape unicode
                                String hex = text.substring(i + 1, i + 5);
                                char[] chars = Character.toChars(Integer.parseInt(hex, 16));
                                r.append(chars);
                                i += 4;
                            } else {
                                // not really unicode
                                r.append("\\").append(cn);
                            }
                            break;
                        }
                        }
                    }
                } else {
                    r.append(c);
                }
            }
            text = r.toString();
        }
        return text;
    }
}

Related

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