Here you can find the source of unescape(String str, char escapeChar, char[] targetChars, char[] escapedChars)
public static String unescape(String str, char escapeChar, char[] targetChars, char[] escapedChars)
//package com.java2s; /*!// ww w . jav a 2s . c o m * mifmi-commons4j * https://github.com/mifmi/mifmi-commons4j * * Copyright (c) 2015 mifmi.org and other contributors * Released under the MIT license * https://opensource.org/licenses/MIT */ public class Main { public static String unescape(String str, char escapeChar, char[] targetChars, char[] escapedChars) { return unescape(str, escapeChar, targetChars, escapedChars, false); } public static String unescape(String str, char escapeChar, char[] targetChars, char[] escapedChars, boolean useUnicodeEscape) { if (str == null) { return null; } int len = str.length(); StringBuilder sb = new StringBuilder(len); unescape(sb, str, escapeChar, targetChars, escapedChars, useUnicodeEscape); return sb.toString(); } public static void unescape(StringBuilder sb, String str, char escapeChar, char[] targetChars, char[] escapedChars) { unescape(sb, str, escapeChar, targetChars, escapedChars, false); } public static void unescape(StringBuilder sb, String str, char escapeChar, char[] targetChars, char[] escapedChars, boolean useUnicodeEscape) { if (targetChars != null && escapedChars != null) { if (targetChars.length != escapedChars.length) { throw new IllegalArgumentException(); } } if (str == null) { return; } int idx = str.indexOf(escapeChar); if (idx == -1) { sb.append(str); return; } int len = str.length(); boolean escape = false; for (int i = 0; i < len; i++) { char ch = str.charAt(i); if (ch == escapeChar) { if (escape) { sb.append(escapeChar); escape = false; } else { escape = true; } } else { if (escape) { int targetIdx = -1; if (escapedChars != null) { for (int j = 0; j < escapedChars.length; j++) { if (ch == escapedChars[j]) { targetIdx = j; break; } } } if (targetIdx == -1) { if (useUnicodeEscape && ch == 'u') { i++; String codePointHexStr = str.substring(i, Math.min(i + 4, len)); i += 3; try { sb.append((char) Integer.parseInt(codePointHexStr, 16)); } catch (NumberFormatException e) { // parse failed sb.append('u'); sb.append(codePointHexStr); } } else if (useUnicodeEscape && ch == 'U') { i++; String codePointHexStr = str.substring(i, Math.min(i + 8, len)); i += 7; try { int codePoint = (int) Long.parseLong(codePointHexStr, 16); sb.appendCodePoint(codePoint); } catch (IllegalArgumentException e) { // parse failed sb.append('U'); sb.append(codePointHexStr); } } else { sb.append(ch); } } else { if (targetChars == null) { sb.append(ch); } else { sb.append(targetChars[targetIdx]); } } escape = false; } else { sb.append(ch); } } } } public static String toString(StringBuilder sb, boolean trim) { if (sb == null) { return null; } if (!trim) { return sb.toString(); } int len = sb.length(); if (len == 0) { return ""; } int sIdx; for (sIdx = 0; sIdx < len; sIdx++) { char ch = sb.charAt(sIdx); if (!Character.isWhitespace(ch)) { break; } } if (sIdx == len) { return ""; } int eIdx; for (eIdx = len - 1; 0 <= eIdx; eIdx--) { char ch = sb.charAt(eIdx); if (!Character.isWhitespace(ch)) { break; } } return sb.substring(sIdx, eIdx + 1); } public static int indexOf(CharSequence charSeq, char ch) { return indexOf(charSeq, ch, 0); } public static int indexOf(CharSequence charSeq, char ch, int fromIndex) { if (charSeq == null) { return -1; } int len = charSeq.length(); for (int i = fromIndex; i < len; i++) { char c = charSeq.charAt(i); if (c == ch) { return i; } } return -1; } public static int indexOf(CharSequence charSeq, char[] ch) { return indexOf(charSeq, ch, 0); } public static int indexOf(CharSequence charSeq, char[] ch, int fromIndex) { if (charSeq == null) { return -1; } int len = charSeq.length(); for (int i = fromIndex; i < len; i++) { char c = charSeq.charAt(i); for (int j = 0; j < ch.length; j++) { if (c == ch[j]) { return i; } } } return -1; } }