Java String Unescape unescapeString(String s)

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

Description

Unescapes an escaped Unicode string.

License

BSD License

Parameter

Parameter Description
s An escaped Unicode string.

Exception

Parameter Description
IllegalArgumentException If the supplied string is not acorrectly escaped N-Triples string.

Return

The unescaped string.

Declaration

public static String unescapeString(String s) 

Method Source Code

    //package com.java2s;
    /*//  w w  w.  j a v  a 2s. c  o m
     * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
     *
     * Licensed under the Aduna BSD-style license.
     */

    public class Main {
        /**
         * Unescapes an escaped Unicode string. Any Unicode sequences
         * (<tt>&#x5C;uxxxx</tt> and <tt>&#x5C;Uxxxxxxxx</tt>) are restored to the
         * value indicated by the hexadecimal argument and any backslash-escapes
         * (<tt>\"</tt>, <tt>\\</tt>, etc.) are decoded to their original form.
         *
         * @param s An escaped Unicode string.
         * @return The unescaped string.
         * @throws IllegalArgumentException If the supplied string is not a
         * correctly escaped N-Triples string.
         */
        public static String unescapeString(String s) {
            int backSlashIdx = s.indexOf('\\');

            if (backSlashIdx == -1) {
                // No escaped characters found
                return s;
            }

            int startIdx = 0;
            int sLength = s.length();
            StringBuilder sb = new StringBuilder(sLength);

            while (backSlashIdx != -1) {
                sb.append(s.substring(startIdx, backSlashIdx));

                if (backSlashIdx + 1 >= sLength) {
                    throw new IllegalArgumentException(
                            "Unescaped backslash in: " + s);
                }

                char c = s.charAt(backSlashIdx + 1);

                if (c == 't') {
                    sb.append('\t');
                    startIdx = backSlashIdx + 2;
                } else if (c == 'r') {
                    sb.append('\r');
                    startIdx = backSlashIdx + 2;
                } else if (c == 'n') {
                    sb.append('\n');
                    startIdx = backSlashIdx + 2;
                } else if (c == '"') {
                    sb.append('"');
                    startIdx = backSlashIdx + 2;
                } else if (c == '\\') {
                    sb.append('\\');
                    startIdx = backSlashIdx + 2;
                } else if (c == 'u') {
                    // \\uxxxx
if (backSlashIdx + 5 >= sLength) {
                        throw new IllegalArgumentException(
                                "Incomplete Unicode escape sequence in: " + s);
                    }
                    String xx = s.substring(backSlashIdx + 2, backSlashIdx + 6);

                    try {
                        c = (char) Integer.parseInt(xx, 16);
                        sb.append(c);

                        startIdx = backSlashIdx + 6;
                    } catch (NumberFormatException e) {
                        throw new IllegalArgumentException(
                                "Illegal Unicode escape sequence '\\u" + xx
                                        + "' in: " + s);
                    }
                } else if (c == 'U') {
                    // \\Uxxxxxxxx
                    if (backSlashIdx + 9 >= sLength) {
                        throw new IllegalArgumentException(
                                "Incomplete Unicode escape sequence in: " + s);
                    }
                    String xx = s
                            .substring(backSlashIdx + 2, backSlashIdx + 10);

                    try {
                        c = (char) Integer.parseInt(xx, 16);
                        sb.append(c);

                        startIdx = backSlashIdx + 10;
                    } catch (NumberFormatException e) {
                        throw new IllegalArgumentException(
                                "Illegal Unicode escape sequence '\\U" + xx
                                        + "' in: " + s);
                    }
                } else {
                    throw new IllegalArgumentException(
                            "Unescaped backslash in: " + s);
                }

                backSlashIdx = s.indexOf('\\', startIdx);
            }

            sb.append(s.substring(startIdx));

            return sb.toString();
        }
    }

Related

  1. unescapeString(String in)
  2. unescapeString(String inp)
  3. unEscapeString(String input)
  4. unescapeString(String input)
  5. unescapeString(String oldstr)
  6. unescapeString(String s)
  7. unescapeString(String s)
  8. unescapeString(String s, char enclosed)
  9. unEscapeString(String str)