- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个字符串如下:
this is the string u00c5 with missing slash before unicode characters
它具有 unicode 字符代码,但缺少“u”之前的所有反斜杠。如何正确打印这个字符串?
我做了什么?
我尝试使用以下代码在不完整的 unicode 部分之前添加反斜杠。但是,replaceAll
中不允许使用 "\u$1"
。
public String sanitizeUnicodeQuirk(String input) {
try {
// String processedInput = input.replaceAll("[uU]([0123456789abcdefABCDEF]{4})", String.valueOf(Integer.parseInt("$1", 16))); // $1 is taken literally which makes valuOf and parseInt useless
String processedInput = input.replaceAll("[uU]([0123456789abcdefABCDEF]{4})", "\\\\u$1"); // Cannot make "\u$1"
String newInput = new String(processedInput.getBytes(), "UTF-8");
return newInput;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return input;
}
最佳答案
哎呀。使用 @AlastairMcCormack 在评论中提供的可能重复链接进行概念证明:
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
代码点被编码为字符串确实有意义,并且使用正则表达式进行任何简单的清理都无法解决这个问题。这不太漂亮,所以如果有人有其他方法,我也会很高兴。
关于java - 如何打印带有缺少反斜杠的unicode字符的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41893798/
我有一个右下角倾斜的元素,我必须在其上放置一个盒子阴影。有时倾斜的 Angular 被徽章覆盖 - 我的问题不适用,如果是这样的话: 这是信息框及其边 Angular 的 (s)css 部分(还有更多
是否可以在纯 html/css 中创建类似下面的内容? 我想做这个响应式和全 (100%) 宽度(最大左 Angular 100 像素,右边最小 50 像素,类似的东西)。 最佳答案 您可以通过转换(
如何在 fabricjs 文本中为文本提供渐变或斜 Angular 效果?? http://fabricjs.com/fabric-intro-part-2/ 这里给出了形状和所有示例,我将其与文本绑
我用过: http://apps.eky.hk/css-triangle-generator/ 为彼此对 Angular 放置的两个不等边三 Angular 形生成 css: 左下三 Angular
我是一名优秀的程序员,十分优秀!