Comparison of strings in different ways in Java

I have two strings, I think I mean according to my eyes, they are exactly the same But their comparison gave me the wrong result

@H_ 502_ 8@

Let me explain briefly, @ h_ 502_ 8@

@H_ 502_ 8@

String bir = "brescia calcio";
String iki = "brescia calcio‎";
 if("brescia calcio".equals("brescia calcio"))
 System.out.println(("deneme"));



HashMap<String,Long> deneme = new HashMap<String,Long>();
HashMap<String,Long> deneme2 = new HashMap<String,Long>();


if (bir.equals(iki)) {
    System.err.println("a");
}
deneme.put(bir,(long) 1);
deneme.put(iki,(long) 2);

deneme2.put("brescia calcio",(long) 3);
deneme2.put("brescia calcio",(long) 4);



System.err.println(deneme.size());
System.err.println(deneme2.size());

The code snippet above generates @ H_ 502_ 8@

deneme 2 1@H_502_8 @

Output I'm completely confused about it Someone can explain why Thank you@ H_ 502_ 8@

Solution

Finally, there is a non printable character that you can't see

@H_ 502_ 8@

@H_ 502_ 8@

String iki = "brescia calcio‎";

for(int i=0;i<iki.length();i++)
    System.out.println(i+": "+iki.charAt(i)+" (" + (int) iki.charAt(i)+")");

Print @ h_ 502_ 8@

@H_ 502_ 8@

0: b (98)
1: r (114)
2: e (101)
3: s (115)
4: c (99)
5: i (105)
6: a (97)
7:   (32)
8: c (99)
9: a (97)
10: l (108)
11: c (99)
12: i (105)
13: o (111)
14: ‎ (8206)
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>