Java String Unescape unEscapeString(String str)

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

Description

Unescape commas in the string using the default escape char

License

Open Source License

Parameter

Parameter Description
str a string

Return

an unescaped string

Declaration

public static String unEscapeString(String str) 

Method Source Code

//package com.java2s;

public class Main {
    final public static char COMMA = ',';
    final public static char ESCAPE_CHAR = '\\';

    /**/*from www . ja va 2s  .  c o m*/
     * Unescape commas in the string using the default escape char
     * 
     * @param str
     *            a string
     * @return an unescaped string
     */
    public static String unEscapeString(String str) {
        return unEscapeString(str, ESCAPE_CHAR, COMMA);
    }

    /**
     * Unescape <code>charToEscape</code> in the string with the escape char <code>escapeChar</code>
     * 
     * @param str
     *            string
     * @param escapeChar
     *            escape char
     * @param charToEscape
     *            the escaped char
     * @return an unescaped string
     */
    public static String unEscapeString(String str, char escapeChar, char charToEscape) {
        return unEscapeString(str, escapeChar, new char[] { charToEscape });
    }

    /**
     * @param charsToEscape
     *            array of characters to unescape
     */
    public static String unEscapeString(String str, char escapeChar, char[] charsToEscape) {
        if (str == null) {
            return null;
        }
        StringBuilder result = new StringBuilder(str.length());
        boolean hasPreEscape = false;
        for (int i = 0; i < str.length(); i++) {
            char curChar = str.charAt(i);
            if (hasPreEscape) {
                if (curChar != escapeChar && !hasChar(charsToEscape, curChar)) {
                    // no special char
                    throw new IllegalArgumentException(
                            "Illegal escaped string " + str + " unescaped " + escapeChar + " at " + (i - 1));
                }
                // otherwise discard the escape char
                result.append(curChar);
                hasPreEscape = false;
            } else {
                if (hasChar(charsToEscape, curChar)) {
                    throw new IllegalArgumentException(
                            "Illegal escaped string " + str + " unescaped " + curChar + " at " + i);
                } else if (curChar == escapeChar) {
                    hasPreEscape = true;
                } else {
                    result.append(curChar);
                }
            }
        }
        if (hasPreEscape) {
            throw new IllegalArgumentException(
                    "Illegal escaped string " + str + ", not expecting " + escapeChar + " in the end.");
        }
        return result.toString();
    }

    private static boolean hasChar(char[] chars, char character) {
        for (char target : chars) {
            if (character == target) {
                return true;
            }
        }
        return false;
    }

    public static String toString(String[] content, String sign) {
        if (null == content) {
            return null;
        }

        sign = null == sign ? "," : sign;
        StringBuilder strBuilder = new StringBuilder();
        for (int i = 0; i < content.length; i++) {
            strBuilder.append(content[i]);
            if (i < content.length - 1) {
                strBuilder.append(sign);
            }
        }

        return strBuilder.toString();
    }
}

Related

  1. unescapeString(String oldstr)
  2. unescapeString(String s)
  3. unescapeString(String s)
  4. unescapeString(String s)
  5. unescapeString(String s, char enclosed)
  6. unEscapeString(String str)
  7. unescapeString(String str)
  8. unEscapeString(String str)
  9. unescapeString(String string)