Java String Unescape unescape(String source)

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

Description

unescape

License

Apache License

Parameter

Parameter Description
source a parameter

Return

String

Declaration

public static String unescape(String source) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from   ww w.  j  a  v a  2  s.  c  o m
     * @param source
     * @return String
     */
    public static String unescape(String source) {
        if (source == null) {
            return "";
        }

        char c;
        StringBuilder buffer = new StringBuilder();

        for (int i = 0, length = source.length(); i < length; i++) {
            c = source.charAt(i);

            if (c == '\\' && (i + 1 < length)) {
                switch (source.charAt(i + 1)) {
                case '\'': {
                    buffer.append("\'");
                    break;
                }
                case '"': {
                    buffer.append("\"");
                    break;
                }
                case 'r': {
                    buffer.append("\r");
                    break;
                }
                case 'n': {
                    buffer.append("\n");
                    break;
                }
                case 't': {
                    buffer.append("\t");
                    break;
                }
                case 'b': {
                    buffer.append("\b");
                    break;
                }
                case 'f': {
                    buffer.append("\f");
                    break;
                }
                case '\\': {
                    buffer.append("\\");
                    break;
                }
                default: {
                    buffer.append('\\');
                    buffer.append(source.charAt(i + 1));
                    break;
                }
                }
                i++;
            } else {
                buffer.append(c);
            }
        }
        return buffer.toString();
    }
}

Related

  1. unescape(String s)
  2. unescape(String s)
  3. unescape(String s, char[] charsToEscape)
  4. unescape(String s, int i)
  5. unescape(String s, String toUnescape)
  6. unescape(String src)
  7. unescape(String src)
  8. unescape(String src)
  9. unescape(String src)