Here you can find the source of unEscapeString(final String src)
Parameter | Description |
---|---|
src | the given source string to unescapes. |
public static String unEscapeString(final String src) throws IllegalStateException
//package com.java2s; /***************************************************************** Copyright 2006 by Dung Nguyen (dungnguyen@truthinet.com) // w ww.ja v a2s. c o m Licensed under the iNet Solutions Corp.,; you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.truthinet.com/licenses Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *****************************************************************/ public class Main { /** comma character. */ public static final char COMMA = ','; /** escape character. */ public static final char ESCAPE = '\\'; /** * Unescapes the given source string. * * @param src the given source string to unescapes. * @return the normal string. */ public static String unEscapeString(final String src) throws IllegalStateException { return unEscapeString(src, ESCAPE, COMMA); } /** * Unescapes the given source string. * * @param src the given source string. * @param escapeChar the given escape character. * @param charsToEscape the given list of characters to escape. * @return the normal string. * @throws IllegalStateException if the string contain invalid escape character. */ public static String unEscapeString(final String src, char escapeChar, char... charsToEscape) throws IllegalStateException { if (src == null) { return null; } final StringBuilder result = new StringBuilder(); boolean hasPreEscape = false; for (int index = 0; index < src.length(); index++) { char curChar = src.charAt(index); // has pre escape. if (hasPreEscape) { if (curChar != escapeChar && !hasChar(curChar, charsToEscape)) { // no special character. throw new IllegalStateException( "Illegal escaped string " + src + " unescaped " + escapeChar + " at " + (index - 1)); } // unescape character. result.append(curChar); hasPreEscape = false; } else { if (hasChar(curChar, charsToEscape)) { throw new IllegalStateException( "Illegal escaped string " + src + " unescaped " + curChar + " at " + (index - 1)); } else if (curChar == escapeChar) { hasPreEscape = true; } else { result.append(curChar); } } } // contain invalid escape character. if (hasPreEscape) { throw new IllegalStateException( "Illegal escape string " + src + " unescaped " + escapeChar + " in the end. "); } // return the result string. return result.toString(); } /** * Check the given character is contains on the list of characters. * * @param character the given character to check. * @param chars the given list of characters. * @return if the given character is contained on the list of characters. */ private static boolean hasChar(char character, char... chars) { if (chars == null) { return false; } for (char ch : chars) { if (ch == character) { return true; } } return false; } }