处理 Java 中具有特殊字符的字符串
Handling Strings with special characters in Java
我正在实现一个字符串匹配算法,该算法需要处理具有特殊字符的字符串。在匹配的一侧,字符串是在 Python 中准备的,然后经过 JAVA。另一方面,他们被另一个环境所准备。现在我在 Java 中的程序中匹配它们(从 JSON 输入检索的字符串)。
虽然处理了一些字符,但我在处理许多其他字符时遇到了问题。
例如,我得到一个 MATCH(两者在我的控制台上都显示为 >> AS IT COMES CRUMBLING
):
"text":"\u003e\u003e AS IT COMES CRUMBLING"
"caption":">> AS IT COMES CRUMBLING"
但这些显示为 不匹配:
"text":"What if you had fewer headaches\nand migraines a month?"
"text":"What if you had fewer headaches\nand migraines a month?"
或者这个:
"text":"Effects of BOTOX® may spread"
"text":"Effects of BOTOX\xc2\xae may spread"
或者这样:
"text":"Let's also rethink how\nwe care for ourselves."
"text":"Let'\xe2\x80\x99s also rethink how\nwe care for ourselves."
在我的代码中,我使用 JSONPath
从两边读取 JSON
个输入,将它们放在一个 ArrayList
中,然后将一个与列表中的所有项目进行比较。
boolean found=false;
myText foundText = null;
for (int i = 0; i < scheduledText.size(); i++) {
if(current.text.equals(scheduledText.get(i).text)) {
found = true;
foundText =scheduledText.get(i);
break;
}
}
if(found)
//print MATCH
else
//print NON_MATCH
我很沮丧。我应该怎么办?我该如何处理这些?
因此,对于我提出的解决方案,您可以在 java 代码中使用一个函数,如下所示。
private static String cleanTextContent(String text)
{
// strips off all non-ASCII characters
text = text.replaceAll("[^\x00-\x7F]", "");
// erases all the ASCII control characters
text = text.replaceAll("[\p{Cntrl}&&[^\r\n\t]]", "");
// removes non-printable characters from Unicode
text = text.replaceAll("\p{C}", "");
text = text.replaceAll("[^ -~]","");
text = text.replaceAll("[^\p{ASCII}]", "");
text = text.replaceAll("\\x\p{XDigit}{2}", "");
text = text.replaceAll("\n","");
text = text.replaceAll("[^\x20-\x7e]", "");
return text.trim();
}
调用此函数后,您可以使用 Apache Commons lib 将字符串转换为 md5 散列,如下所示。
private static String hashMyString(String text) {
String hashText= text;
String md5Hex = DigestUtils
.md5Hex(hashText).toUpperCase();
return md5Hex;
}
最后只比较主程序中的两个哈希。
编辑: 如果使用 Maven,这是基本上使 DigestUtils 工作的库。
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
编辑:
我的完整字符串测试代码。
public class App
{
public static void main( String[] args ) throws UnsupportedEncodingException
{
String sideOneString = "Effects of BOTOX® may spread";
String sideTwoString = "Effects of BOTOX\xc2\xae may spread";
String sideThreeString = "BOTOX injections take about\n15 mins";
String sideFourString = "BOTOX\xc2\xae injections take about\n15 mins";
System.out.println( hashMyString(cleanTextContent(sideOneString)));
System.out.println( hashMyString(cleanTextContent(sideTwoString)));
System.out.println( hashMyString(cleanTextContent(sideThreeString)));
System.out.println( hashMyString(cleanTextContent(sideFourString)));
}
private static String hashMyString(String text) {
String hashText= text;
String md5Hex = DigestUtils.md5Hex(hashText).toUpperCase();
//System.out.println(md5Hex);
return md5Hex;
}
private static String cleanTextContent(String text)
{
// strips off all non-ASCII characters
text = text.replaceAll("[^\x00-\x7F]", "");
// erases all the ASCII control characters
text = text.replaceAll("[\p{Cntrl}&&[^\r\n\t]]", "");
// removes non-printable characters from Unicode
text = text.replaceAll("\p{C}", "");
text = text.replaceAll("[^ -~]","");
text = text.replaceAll("[^\p{ASCII}]", "");
text = text.replaceAll("\\x\p{XDigit}{2}", "");
text = text.replaceAll("\\n","");
text = text.replaceAll("[^\x20-\x7e]", "");
return text.trim();
}
}
结果:
F928A529F380EB59575AC8A175FDFE79
F928A529F380EB59575AC8A175FDFE79
B4740299C53E18C9ECAF18BA35151D43
B4740299C53E18C9ECAF18BA35151D43
我正在实现一个字符串匹配算法,该算法需要处理具有特殊字符的字符串。在匹配的一侧,字符串是在 Python 中准备的,然后经过 JAVA。另一方面,他们被另一个环境所准备。现在我在 Java 中的程序中匹配它们(从 JSON 输入检索的字符串)。
虽然处理了一些字符,但我在处理许多其他字符时遇到了问题。
例如,我得到一个 MATCH(两者在我的控制台上都显示为 >> AS IT COMES CRUMBLING
):
"text":"\u003e\u003e AS IT COMES CRUMBLING"
"caption":">> AS IT COMES CRUMBLING"
但这些显示为 不匹配:
"text":"What if you had fewer headaches\nand migraines a month?"
"text":"What if you had fewer headaches\nand migraines a month?"
或者这个:
"text":"Effects of BOTOX® may spread"
"text":"Effects of BOTOX\xc2\xae may spread"
或者这样:
"text":"Let's also rethink how\nwe care for ourselves."
"text":"Let'\xe2\x80\x99s also rethink how\nwe care for ourselves."
在我的代码中,我使用 JSONPath
从两边读取 JSON
个输入,将它们放在一个 ArrayList
中,然后将一个与列表中的所有项目进行比较。
boolean found=false;
myText foundText = null;
for (int i = 0; i < scheduledText.size(); i++) {
if(current.text.equals(scheduledText.get(i).text)) {
found = true;
foundText =scheduledText.get(i);
break;
}
}
if(found)
//print MATCH
else
//print NON_MATCH
我很沮丧。我应该怎么办?我该如何处理这些?
因此,对于我提出的解决方案,您可以在 java 代码中使用一个函数,如下所示。
private static String cleanTextContent(String text)
{
// strips off all non-ASCII characters
text = text.replaceAll("[^\x00-\x7F]", "");
// erases all the ASCII control characters
text = text.replaceAll("[\p{Cntrl}&&[^\r\n\t]]", "");
// removes non-printable characters from Unicode
text = text.replaceAll("\p{C}", "");
text = text.replaceAll("[^ -~]","");
text = text.replaceAll("[^\p{ASCII}]", "");
text = text.replaceAll("\\x\p{XDigit}{2}", "");
text = text.replaceAll("\n","");
text = text.replaceAll("[^\x20-\x7e]", "");
return text.trim();
}
调用此函数后,您可以使用 Apache Commons lib 将字符串转换为 md5 散列,如下所示。
private static String hashMyString(String text) {
String hashText= text;
String md5Hex = DigestUtils
.md5Hex(hashText).toUpperCase();
return md5Hex;
}
最后只比较主程序中的两个哈希。
编辑: 如果使用 Maven,这是基本上使 DigestUtils 工作的库。
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
编辑: 我的完整字符串测试代码。
public class App
{
public static void main( String[] args ) throws UnsupportedEncodingException
{
String sideOneString = "Effects of BOTOX® may spread";
String sideTwoString = "Effects of BOTOX\xc2\xae may spread";
String sideThreeString = "BOTOX injections take about\n15 mins";
String sideFourString = "BOTOX\xc2\xae injections take about\n15 mins";
System.out.println( hashMyString(cleanTextContent(sideOneString)));
System.out.println( hashMyString(cleanTextContent(sideTwoString)));
System.out.println( hashMyString(cleanTextContent(sideThreeString)));
System.out.println( hashMyString(cleanTextContent(sideFourString)));
}
private static String hashMyString(String text) {
String hashText= text;
String md5Hex = DigestUtils.md5Hex(hashText).toUpperCase();
//System.out.println(md5Hex);
return md5Hex;
}
private static String cleanTextContent(String text)
{
// strips off all non-ASCII characters
text = text.replaceAll("[^\x00-\x7F]", "");
// erases all the ASCII control characters
text = text.replaceAll("[\p{Cntrl}&&[^\r\n\t]]", "");
// removes non-printable characters from Unicode
text = text.replaceAll("\p{C}", "");
text = text.replaceAll("[^ -~]","");
text = text.replaceAll("[^\p{ASCII}]", "");
text = text.replaceAll("\\x\p{XDigit}{2}", "");
text = text.replaceAll("\\n","");
text = text.replaceAll("[^\x20-\x7e]", "");
return text.trim();
}
}
结果:
F928A529F380EB59575AC8A175FDFE79
F928A529F380EB59575AC8A175FDFE79
B4740299C53E18C9ECAF18BA35151D43
B4740299C53E18C9ECAF18BA35151D43