Java Java String Unescape unescapeJava(String s)

Here you can find the source of unescapeJava(String s)

Description

Turns a printable Java string literal (minus the quotes) into a string.

License

Open Source License

Declaration

public static String unescapeJava(String s) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /** Turns a printable Java string literal (minus the quotes) into a string. So "\t" is converted to a tab, et cetera. */
    public static String unescapeJava(String s) {
        final int sLength = s.length();
        final StringBuilder result = new StringBuilder(sLength);
        for (int i = 0; i < sLength; ++i) {
            final char c = s.charAt(i);
            if (c == '\\' && i < sLength - 1) {
                char next = s.charAt(++i);
                if (next == 'n') {
                    result.append('\n');
                } else if (next == 'r') {
                    result.append('\r');
                } else if (next == 't') {
                    result.append('\t');
                } else if (next == 'u') {
                    char actualChar = (char) Integer.parseInt(s.substring(++i, i + 4), 16);
                    i += 3;//from  www. j  a  v  a2s .  com
                    result.append(actualChar);
                } else {
                    result.append(next);
                }
            } else {
                result.append(c);
            }
        }
        return result.toString();
    }
}

Related

  1. unescapeJava(final String s)
  2. unescapeJava(final String str)
  3. unescapeJava(final String str)
  4. unescapeJava(String escaped)
  5. unescapeJava(String str)
  6. unescapeJava(String value)