Java String Unescape unescape(String line)

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

Description

unescape

License

Apache License

Declaration

public static String unescape(String line) 

Method Source Code

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

public class Main {
    public static String unescape(String line) {
        if (line == null) {
            return null;
        }//from   w  ww.  ja  v a  2 s  .c om
        StringBuffer sb = new StringBuffer();
        for (int i = 0, n = line.length(); i < n; i++) {
            char c = line.charAt(i);
            if (c == '\\') {
                if (i == n - 1) {
                    throw new IllegalArgumentException("Invalid string to unescape:" + line);
                }
                char d = line.charAt(i + 1);
                switch (d) {
                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;
                default:
                    throw new IllegalArgumentException("Invalid string to unescape:" + line);
                }
                i++;
            } else {
                sb.append(c);
            }
        }
        return new String(sb);
    }
}

Related

  1. unescape(String escaped, char escape)
  2. unescape(String escapedMessage)
  3. unescape(String input)
  4. unescape(String input)
  5. unescape(String input, char escapeChar)
  6. unescape(String line)
  7. unescape(String line, int start, int end)
  8. unescape(String literal, String escapedChars)
  9. unescape(String name)