Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中将unicode字符串转换为另一种格式?_Java_Regex - Fatal编程技术网

如何在Java中将unicode字符串转换为另一种格式?

如何在Java中将unicode字符串转换为另一种格式?,java,regex,Java,Regex,我想查询像“Morristown\\u002c\u News\u Jersey”这样的数据。因为在我的数据库中,“Morristown\\u002c\u News\u Jersey”类似于“Morristown\$002C\u News\u Jersey”。“\\u”变为“\$”,unicode中的小写变为大写。是否有一种简洁的方法来处理所有类型的数据?以下是使用regex\\\\u([^\u]+)的Java代码,用DB等效性替换Unicode字符代码: String str = "Jos\\u

我想查询像“Morristown\\u002c\u News\u Jersey”这样的数据。因为在我的数据库中,
“Morristown\\u002c\u News\u Jersey”
类似于
“Morristown\$002C\u News\u Jersey”
“\\u”
变为
“\$”
,unicode中的小写变为大写。是否有一种简洁的方法来处理所有类型的数据?

以下是使用regex
\\\\u([^\u]+)
的Java代码,用DB等效性替换Unicode字符代码:

String str = "Jos\\u00e9_A\\u002e_Santos";
Matcher matcher = Pattern.compile("\\\\u([^_]+)").matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
  matcher.appendReplacement(sb, "\\\\\\$" + matcher.group(1).toUpperCase());
}
matcher.appendTail(sb);
System.out.println("The original string "+ str+ "\n has been converted "+ sb.toString());
印刷品:

The original string Jos\u00e9_A\u002e_Santos
 has been converted Jos\$00E9_A\$002E_Santos
代码随类文档一起提供。


只需使用
String.replaceAll(“\\$”,“\\u”)看起来您应该修复代码以将数据插入数据库,而不是修复损坏的文本…您好,我使用了您的解决方案,但它有一个bug。当遇到“Jos\\u00e9\U A\\u002e\U Santos”这样的情况时,它将导致无限循环。针对多个匹配更新