Java Invisible character processing

Source: Internet
Author: User

When visiting the blog park, I saw an article that compares two strings for equality, compares two strings, and uses one night.

string m1 = "fa‌?c5‌?04‌?6b‌?c1‌?0a‌?ca‌?2e‌?1e‌?a9‌?55‌?eb‌?e4‌?53‌?7d‌?c0";string m2 = "fac5046bc10aca2e1ea955ebe4537dc0";

are not equal. Because the M1 contains invisible characters (Zero-width Non-joiner (ZWNJ), Zero-width Space (ZWSP), both of them)
Workaround:
①. Copy to QQ, you can see the difference.
②. Using Java code to differentiate
1String m1 = "fa c5? 6b? C1 0a?" CA? 2e 1e? A9? eb? e4? 7d? c0;2String m2 = "fac5046bc10aca2e1ea955ebe4537dc0";3SYSTEM.OUT.PRINTLN ("M1 Length:" +m1.length () + "\ n" + "m2 Length:" +m2.length ());4  byte[] bs =m1.getbytes ();5System.out.print ("M1:");6   for(byteb:bs) {7System.out.print (b + ""));8    }9 System.out.println ();TenString all = M1.replaceall ("\\p{c}", ""); OneSystem.out.println (M2.equals (All));

Get the output result:

Length of M1:length of m2:M1:
-30-128-116-30-128-117 48 97-30-128-116-30-128-117 99 97-30-128-116-30-128-117 50 101-30-128-116-30- 128-117 49 101-30-128
-116-30-128-117 97 57-30-128-116-30-128-117 53 53-30-128-116-30-128-117 101 98-30-128-116-30-128-117 101 52-30-128-116-30
True

Java Invisible character processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.