Java String Unescape unescapeString(String inp)

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

Description

Removes the first level of escapes from a string

License

Open Source License

Parameter

Parameter Description
inp the string to unescape

Return

the unescaped string

Declaration

public static String unescapeString(String inp) 

Method Source Code

//package com.java2s;
/*//w ww  .ja v  a  2s . c o m
 *EXHIBIT A - Sun Industry Standards Source License
 *
 *"The contents of this file are subject to the Sun Industry
 *Standards Source License Version 1.2 (the "License");
 *You may not use this file except in compliance with the
 *License. You may obtain a copy of the 
 *License at http://wbemservices.sourceforge.net/license.html
 *
 *Software distributed under the License is distributed on
 *an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
 *express or implied. See the License for the specific
 *language governing rights and limitations under the License.
 *
 *The Original Code is WBEM Services.
 *
 *The Initial Developer of the Original Code is:
 *Sun Microsystems, Inc.
 *
 *Portions created by: Sun Microsystems, Inc.
 *are Copyright 2002 Sun Microsystems, Inc.
 *
 *All Rights Reserved.
 *
 *Contributor(s): Brian Schlosser
 */

public class Main {
    /**
     * Removes the first level of escapes from a string
     *
     * @param inp the string to unescape
     * @return the unescaped string 
     */
    public static String unescapeString(String inp) {
        StringBuffer sb = new StringBuffer();
        int size = inp.length();
        for (int i = 0; i < size; i++) {
            char ch = inp.charAt(i);
            if (ch == '\\') {
                i++;
                if (i >= size) {
                    throw new IllegalArgumentException(
                            "String ended with an escape, but there was no subsequent character to escape");
                }
                ch = inp.charAt(i);
                switch (ch) {
                case 'n':
                    sb.append('\n');
                    break;
                case 't':
                    sb.append('\t');
                    break;
                case 'b':
                    sb.append('\b');
                    break;
                case 'r':
                    sb.append('\r');
                    break;
                case 'f':
                    sb.append('\f');
                    break;
                case '\\':
                case '\'':
                case '\"':
                    sb.append(ch);
                    break;
                case 'X':
                case 'x':
                    sb.append("\\x");
                    //?? Finish this
                    //                        int j = i;
                    //                        while(Character.digit(inp.charAt(j), 16) >= 0 && 
                    //                              j < (i + 4))
                    //                        {
                    //                            j++;
                    //                        }
                    //                        int value = Integer.parseInt(inp.substring(i, j), 16);
                    //                        i = j;  
                    //                        sb.append((char)value);
                    break;
                default:
                    throw new IllegalArgumentException("Invalid escape sequence '" + ch
                            + "' (valid sequences are  \\b  \\t  \\n  \\f  \\r  \\\"  \\\'  \\\\ \\x0000 \\X0000 )");
                }
            } else {
                sb.append(ch);
            }
        }
        return sb.toString();
    }
}

Related

  1. unescape(StringBuilder escapedText, int index)
  2. unEscapeString(final String src)
  3. unescapeString(String escapedString)
  4. unescapeString(String iccProfileSrc)
  5. unescapeString(String in)
  6. unescapeString(String input)
  7. unEscapeString(String input)
  8. unescapeString(String oldstr)
  9. unescapeString(String s)