Here you can find the source of unescape(final String str, final char escapeChar)
Parameter | Description |
---|---|
str | the escaped string to be unescaped. |
escapeChar | the character used to escape itself and other characters. |
public static String unescape(final String str, final char escapeChar)
//package com.java2s; /*//w w w. ja v a 2 s . c o m * Copyright (c) 2014 Haixing Hu * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * 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 { /** * Unescapes characters in a escaped string. * <p> * All characters in the escaped string escaped by the specified escape * character will be unescaped. * * @param str * the escaped string to be unescaped. * @param escapeChar * the character used to escape itself and other characters. * @return the unescaped string. */ public static String unescape(final String str, final char escapeChar) { final StringBuilder builder = new StringBuilder(str.length()); unescape(str, escapeChar, builder); return builder.toString(); } /** * Unescapes characters in a escaped string. * <p> * All characters in the escaped string escaped by the specified escape * character will be unescaped. * * @param str * the escaped string to be unescaped. * @param escapeChar * the character used to escape itself and other characters. * @param builder * a string builder where to append the unescaped string. */ public static void unescape(final String str, final char escapeChar, final StringBuilder builder) { final int n = str.length(); boolean escaped = false; for (int i = 0; i < n; ++i) { final char ch = str.charAt(i); if (escaped) { builder.append(ch); escaped = false; } else if (ch == escapeChar) { escaped = true; } else { builder.append(ch); } } } }