Java String Unescape unescape(String str)

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

Description

Unescapes the given string and returns the result.

License

Open Source License

Parameter

Parameter Description
str the string to unescape

Return

an unescaped string

Declaration

public static String unescape(String str) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005,2007 Cognium Systems SA and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License, Version 2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0
 * /*from  w w w  .jav a  2s.c  o  m*/
 * Contributors:
 *     Cognium Systems SA - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * The default character to use has escaping char.
     */
    private static final char DEFAULT_ESCAPECHAR = '\u005c\u005c';

    /**
     * Unescapes the given string and returns the result. This method uses the
     * default escape symbol (see {@link #DEFAULT_ESCAPECHAR}).
     * 
     * @param str the string to unescape
     * @return an unescaped string
     */
    public static String unescape(String str) {
        return unescape(str, DEFAULT_ESCAPECHAR);
    }

    /**
     * Unescapes the given string and returns the result.
     * 
     * @param str the string to unescape
     * @param escape the symbol used to escape characters
     * @return an unescaped string
     */
    public static String unescape(String str, char escape) {
        if (str == null)
            return "";
        StringBuffer buf = new StringBuffer();
        char[] array = str.toCharArray();
        boolean escaped = false;
        for (int i = 0; i < array.length; i++) {
            char ch = array[i];
            if (escaped) {
                buf.append(ch);
                escaped = false;
            } else {
                escaped = (ch == escape);
                if (!escaped)
                    buf.append(ch);
            }
        }
        return buf.toString();
    }
}

Related

  1. unescape(String str)
  2. unescape(String str)
  3. unescape(String str)
  4. unescape(String str)
  5. unescape(String str)
  6. unescape(String str)
  7. unescape(String str, char escapeChar, char[] targetChars, char[] escapedChars)
  8. unescape(String string)
  9. unescape(String string)