Java Java String Unescape unescapeJava(final String str)

Here you can find the source of unescapeJava(final String str)

Description

unescape Java

License

Open Source License

Declaration

public static String unescapeJava(final String str) 

Method Source Code

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

public class Main {
    public static String unescapeJava(final String str) {
        final char[] chars = str.toCharArray();
        final StringBuilder sb = new StringBuilder(str.length());
        for (int i = 0; i < chars.length; ++i) {
            char c = chars[i];
            if (c != '\\') {
                sb.append(c);/*w  ww  .j ava  2  s.  co m*/
            } else {
                ++i;
                c = chars[i];
                switch (c) {
                case 'b': {
                    sb.append('\b');
                    break;
                }
                case 't': {
                    sb.append('\t');
                    break;
                }
                case 'n': {
                    sb.append('\n');
                    break;
                }
                case 'f': {
                    sb.append('\f');
                    break;
                }
                case 'r': {
                    sb.append('\r');
                    break;
                }
                case '\"': {
                    sb.append('\"');
                    break;
                }
                case '\\': {
                    sb.append('\\');
                    break;
                }
                case 'u': {
                    final char hex = (char) Integer.parseInt(new String(chars, i + 1, 4), 16);
                    sb.append(hex);
                    i += 4;
                    break;
                }
                default: {
                    throw new IllegalArgumentException("Invalid escaping character: " + c);
                }
                }
            }
        }
        return sb.toString();
    }

    public static String toString(final Object value) {
        if (value == null) {
            return null;
        }
        return value.toString();
    }
}

Related

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