如何打印缺少反斜杠的unicode字符的字符串?
How to print string with unicode characters missing backslashes?
我有一个字符串如下:
this is the string u00c5 with missing slash before unicode characters
它有 unicode 字符代码,但缺少 "u" 之前的所有反斜杠。如何正确打印这个字符串?
我做了什么?
我尝试使用以下代码在不完整的 unicode 部分之前添加反斜杠。但是,"\u"
在 replaceAll
中是不允许的。
public String sanitizeUnicodeQuirk(String input) {
try {
// String processedInput = input.replaceAll("[uU]([0123456789abcdefABCDEF]{4})", String.valueOf(Integer.parseInt("", 16))); // is taken literally which makes valuOf and parseInt useless
String processedInput = input.replaceAll("[uU]([0123456789abcdefABCDEF]{4})", "\\u"); // Cannot make "\u"
String newInput = new String(processedInput.getBytes(), "UTF-8");
return newInput;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return input;
}
哎呀。使用@AlastairMcCormack 在评论中提供的可能重复项 link 的概念证明:
public class Test {
public static void main(String[] args) {
String input = "this is the string u0075u0031u0032u0033u0034 with missing slash before unicode characters";
System.out.println("Original input: " + input);
Pattern pattern = java.util.regex.Pattern.compile("[uU][0-9a-fA-F]{4}");
Matcher matcher = pattern.matcher(input);
StringBuilder builder = new StringBuilder();
int lastIndex = 0;
while (matcher.find()) {
String codePoint = matcher.group().substring(1);
System.out.println("Found code point: " + codePoint);
Character charSymbol = (char) Integer.parseInt(codePoint, 16);
builder.append(input.substring(lastIndex, matcher.start()) + charSymbol);
lastIndex = matcher.end();
}
builder.append(input.substring(lastIndex));
System.out.println("Modded input: " + builder.toString());
}
}
产量:
Original input: this is the string u0075u0031u0032u0033u0034 with missing slash before unicode characters
Found code point: 0075
Found code point: 0031
Found code point: 0032
Found code point: 0033
Found code point: 0034
Modded input: this is the string u1234 with missing slash before unicode characters
将代码点编码为字符串确实很有意义,使用正则表达式进行简单的清理是无法解决这个问题的。它不是很漂亮,所以如果有人有其他方法,我也会很高兴。
我有一个字符串如下:
this is the string u00c5 with missing slash before unicode characters
它有 unicode 字符代码,但缺少 "u" 之前的所有反斜杠。如何正确打印这个字符串?
我做了什么?
我尝试使用以下代码在不完整的 unicode 部分之前添加反斜杠。但是,"\u"
在 replaceAll
中是不允许的。
public String sanitizeUnicodeQuirk(String input) {
try {
// String processedInput = input.replaceAll("[uU]([0123456789abcdefABCDEF]{4})", String.valueOf(Integer.parseInt("", 16))); // is taken literally which makes valuOf and parseInt useless
String processedInput = input.replaceAll("[uU]([0123456789abcdefABCDEF]{4})", "\\u"); // Cannot make "\u"
String newInput = new String(processedInput.getBytes(), "UTF-8");
return newInput;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return input;
}
哎呀。使用@AlastairMcCormack 在评论中提供的可能重复项 link 的概念证明:
public class Test {
public static void main(String[] args) {
String input = "this is the string u0075u0031u0032u0033u0034 with missing slash before unicode characters";
System.out.println("Original input: " + input);
Pattern pattern = java.util.regex.Pattern.compile("[uU][0-9a-fA-F]{4}");
Matcher matcher = pattern.matcher(input);
StringBuilder builder = new StringBuilder();
int lastIndex = 0;
while (matcher.find()) {
String codePoint = matcher.group().substring(1);
System.out.println("Found code point: " + codePoint);
Character charSymbol = (char) Integer.parseInt(codePoint, 16);
builder.append(input.substring(lastIndex, matcher.start()) + charSymbol);
lastIndex = matcher.end();
}
builder.append(input.substring(lastIndex));
System.out.println("Modded input: " + builder.toString());
}
}
产量:
Original input: this is the string u0075u0031u0032u0033u0034 with missing slash before unicode characters
Found code point: 0075
Found code point: 0031
Found code point: 0032
Found code point: 0033
Found code point: 0034
Modded input: this is the string u1234 with missing slash before unicode characters
将代码点编码为字符串确实很有意义,使用正则表达式进行简单的清理是无法解决这个问题的。它不是很漂亮,所以如果有人有其他方法,我也会很高兴。