Java String Unescape unescapeString(String string, String escapeChars, char escapeSymbol)

Here you can find the source of unescapeString(String string, String escapeChars, char escapeSymbol)

Description

Removes occurrences of an escape character from a string.

License

Open Source License

Parameter

Parameter Description
string the string to process.
escapeChars the characters that are to be unescaped. If null, all characters preceded by the escape symbol will be escaped. If empty, no escape characters are unescaped.
escapeSymbol the symbol that appears before escaped characters.

Return

the string with all escape sequences eliminated.

Declaration

public static String unescapeString(String string, String escapeChars, char escapeSymbol) 

Method Source Code

//package com.java2s;
/*//from   w w  w . j  a v a 2  s.  c om
 * TextUtilities.java (Class: com.madphysicist.tools.util.TextUtilities)
 *
 * Mad Physicist JTools Project (General Purpose Utilities)
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2012 by Joseph Fox-Rabinovitz
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

public class Main {
    /**
     * Removes occurrences of an escape character from a string. If a list of
     * escape characters is specified, only the characters in that list are
     * regarded as escape sequences when preceded by an escape symbol.
     *
     * @param string the string to process.
     * @param escapeChars the characters that are to be unescaped. If null, all
     * characters preceded by the escape symbol will be escaped. If empty, no
     * escape characters are unescaped.
     * @param escapeSymbol the symbol that appears before escaped characters.
     * @return the string with all escape sequences eliminated.
     * @since 1.0.0
     */
    public static String unescapeString(String string, String escapeChars, char escapeSymbol) {
        if (string == null || string.isEmpty() || (escapeChars != null && escapeChars.isEmpty())) {
            return string;
        }
        StringBuilder sb = new StringBuilder(string.length());
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            if (c == escapeSymbol && i < string.length() - 1) {
                char c2 = string.charAt(i + 1);
                if (escapeChars == null || escapeChars.indexOf(c2) >= 0) {
                    c = c2;
                    i++;
                }
            }
            sb.append(c);
        }
        string = sb.toString();
        return sb.toString();
    }
}

Related

  1. unEscapeString(String str)
  2. unescapeString(String str)
  3. unEscapeString(String str)
  4. unEscapeString(String str)
  5. unescapeString(String string)
  6. unescapeString(String t)
  7. unescapeString(String text)
  8. unescapeString(String text)
  9. unescapeString(String text)