К сожалению, удаление одной обратной реакции, как указано в первом комментарии (newbiedoodle), не приводит к хорошему результату. Большинство (если не все) IDE выдает синтаксическую ошибку. Причина в том, что формат Java Escaped Unicode предполагает синтаксис "\ uXXXX", где XXXX - это 4 шестнадцатеричные цифры, которые являются обязательными. Попытки сложить эту строку из кусочков не удаются. Конечно, "\ u" - это не то же самое, что "\\ u". Первый синтаксис означает экранирование 'u', второй означает экранирование (обратная реакция), за которым следует 'u'. Странно, что на страницах Apache представлена утилита, которая делает именно это поведение. Но на самом деле это Escape Mimic Utility . У Apache есть свои утилиты (я их не тестировал), которые делают эту работу за вас. Может быть, это все еще не то, что вы хотите иметь. Утилиты Apache Escape Unicode Но эта утилита 1 имеет хороший подход к решению. С комбинацией, описанной выше (MeraNaamJoker). Мое решение состоит в том, чтобы создать эту имитированную строку Escaped и затем преобразовать ее обратно в Unicode (чтобы избежать реального ограничения Escaped Unicode). Я использовал его для копирования текста, поэтому возможно, что в методе uencode будет лучше использовать '\\ u', кроме '\\\\ u'. Попробуй.
/**
* Converts character to the mimic unicode format i.e. '\\u0020'.
*
* This format is the Java source code format.
*
* CharUtils.unicodeEscaped(' ') = "\\u0020"
* CharUtils.unicodeEscaped('A') = "\\u0041"
*
* @param ch the character to convert
* @return is in the mimic of escaped unicode string,
*/
public static String unicodeEscaped(char ch) {
String returnStr;
//String uniTemplate = "\u0000";
final static String charEsc = "\\u";
if (ch < 0x10) {
returnStr = "000" + Integer.toHexString(ch);
}
else if (ch < 0x100) {
returnStr = "00" + Integer.toHexString(ch);
}
else if (ch < 0x1000) {
returnStr = "0" + Integer.toHexString(ch);
}
else
returnStr = "" + Integer.toHexString(ch);
return charEsc + returnStr;
}
/**
* Converts the string from UTF8 to mimic unicode format i.e. '\\u0020'.
* notice: i cannot use real unicode format, because this is immediately translated
* to the character in time of compiling and editor (i.e. netbeans) checking it
* instead reaal unicode format i.e. '\u0020' i using mimic unicode format '\\u0020'
* as a string, but it doesn't gives the same results, of course
*
* This format is the Java source code format.
*
* CharUtils.unicodeEscaped(' ') = "\\u0020"
* CharUtils.unicodeEscaped('A') = "\\u0041"
*
* @param String - nationalString in the UTF8 string to convert
* @return is the string in JAVA unicode mimic escaped
*/
public String encodeStr(String nationalString) throws UnsupportedEncodingException {
String convertedString = "";
for (int i = 0; i < nationalString.length(); i++) {
Character chs = nationalString.charAt(i);
convertedString += unicodeEscaped(chs);
}
return convertedString;
}
/**
* Converts the string from mimic unicode format i.e. '\\u0020' back to UTF8.
*
* This format is the Java source code format.
*
* CharUtils.unicodeEscaped(' ') = "\\u0020"
* CharUtils.unicodeEscaped('A') = "\\u0041"
*
* @param String - nationalString in the JAVA unicode mimic escaped
* @return is the string in UTF8 string
*/
public String uencodeStr(String escapedString) throws UnsupportedEncodingException {
String convertedString = "";
String[] arrStr = escapedString.split("\\\\u");
String str, istr;
for (int i = 1; i < arrStr.length; i++) {
str = arrStr[i];
if (!str.isEmpty()) {
Integer iI = Integer.parseInt(str, 16);
char[] chaCha = Character.toChars(iI);
convertedString += String.valueOf(chaCha);
}
}
return convertedString;
}