Java String Unescape unescape(String aString)

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

Description

Remove "escaped" chars from a string.

License

Open Source License

Declaration

public static String unescape(String aString) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2001, 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  w  w  w  .  j  av  a  2  s  . com*/
 *     IBM Corporation - initial API and implementation
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/

public class Main {
    protected static final String CARRIAGE_RETURN = "\r";
    protected static final String CARRIAGE_RETURN_ENTITY = "\\r";
    protected static final String EQUAL_SIGN = "=";
    protected static final String EQUAL_SIGN_ENTITY = "=";
    protected static final String LINE_FEED = "\n";
    protected static final String LINE_FEED_ENTITY = "\\n";
    protected static final String LINE_TAB = "\t";
    protected static final String LINE_TAB_ENTITY = "\\t";

    /**
     * Remove "escaped" chars from a string.
     */
    public static String unescape(String aString) {
        if (aString == null)
            return null;
        String normalString = replace(aString, EQUAL_SIGN_ENTITY, EQUAL_SIGN);
        normalString = replace(normalString, LINE_FEED_ENTITY, LINE_FEED);
        normalString = replace(normalString, CARRIAGE_RETURN_ENTITY, CARRIAGE_RETURN);
        normalString = replace(normalString, LINE_TAB_ENTITY, LINE_TAB);
        return normalString;
    }

    /**
     * Replace matching literal portions of a string with another string
     */
    public static String replace(String aString, String source, String target) {
        if (aString == null)
            return null;
        String normalString = ""; //$NON-NLS-1$
        int length = aString.length();
        int position = 0;
        int previous = 0;
        int spacer = source.length();
        while (position + spacer - 1 < length && aString.indexOf(source, position) > -1) {
            position = aString.indexOf(source, previous);
            normalString = normalString + aString.substring(previous, position) + target;
            position += spacer;
            previous = position;
        }
        normalString = normalString + aString.substring(position, aString.length());

        return normalString;
    }
}

Related

  1. unescape(final String source)
  2. unescape(final String str, final char escapeChar)
  3. unescape(final String string, final char quoteChar)
  4. unescape(final String text)
  5. unescape(final String[] inval, final Character escapeChar)
  6. unescape(String aValue)
  7. unescape(String data)
  8. unescape(String elt)
  9. unescape(String entry)