繁体   English   中英

使用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 hash这样的东西。

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>

编辑:我的完整测试代码为String。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM