Java String Unescape unescape(String val, char quote)

Here you can find the source of unescape(String val, char quote)

Description

Unescape escaped chars in a string 'val'.

License

Open Source License

Declaration

static public String unescape(String val, char quote) 

Method Source Code

//package com.java2s;
/*/* ww  w.j  a va  2s .com*/
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of the License "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: 
*
*/

public class Main {
    /**
    * Unescape escaped chars in a string 'val'.
    * 
    */
    static public String unescape(String val, char quote) {
        StringBuffer buff = new StringBuffer();
        for (int i = 0; i < val.length(); i++) {
            char ch = val.charAt(i);
            if (ch == '\\' && i + 1 < val.length()) {
                ch = val.charAt(++i);
                if (Character.digit(ch, 8) >= 0 && i + 2 < val.length()) {
                    String code = val.substring(i, i + 3);
                    i += 2;
                    try {
                        int octal = Integer.parseInt(code, 8);
                        buff.append((char) octal);
                    } catch (NumberFormatException x) {
                        //                      UtilsPlugin.log(x);
                    }
                } else {
                    switch (ch) {
                    case 't':
                        buff.append('\t');
                        break;
                    case 'n':
                        buff.append('\n');
                        break;
                    case 'r':
                        buff.append('\r');
                        break;
                    case 'f':
                        buff.append('\f');
                        break;
                    case 'b':
                        buff.append('\b');
                        break;
                    case 'u':
                        try {
                            String code = val.substring(i + 1, i + 5);
                            buff.append((char) Integer.parseInt(code, 16));
                            i += 4;
                        } catch (NumberFormatException e) {
                            buff.append('\\');
                            buff.append('u');
                        }
                        break;
                    case '\\':
                        buff.append('\\');
                        break;
                    case '"':
                        buff.append('"');
                        break;
                    case '\'':
                        buff.append('\'');
                        break;
                    default:
                        buff.append('\\');
                        buff.append(ch);
                        break;
                    }
                }
            } else {
                buff.append(ch);
            }
        }
        return buff.toString();
    }

    /**
     * Parse a string into an integer, returning a default
     * value instead of throwing an exception for invalid strings.
     */
    public static int parseInt(String s, int defaultValue) {
        int result = defaultValue;
        // avoid spurious IllegalArgumentException
        if (s != null) {
            try {
                result = Integer.parseInt(s);
            } catch (NumberFormatException x) {
            }
        }
        return result;
    }
}

Related

  1. unescape(String text)
  2. unescape(String text)
  3. unescape(String text)
  4. unescape(String theValue)
  5. unescape(String val)
  6. unescape(String value)
  7. unescape(String value)
  8. unescape(String value)
  9. unescape(String value)