Difference comparing strings differently in java

I have two lines and I think they are exactly the same from my eyes. But comparing them gives me the wrong result.

let me explain briefly,

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 piece of code above produces

deneme
2
1

output. I was completely confused. can someone explain why this is the case. thank.

+3


source to share


2 answers


There is an unprintable character at the end that you cannot see.

String iki = "brescia calcio‎";

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

      



prints

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)

      

+2


source


In your second line iki

, you have a non-printable character at the end. When I copy and paste this line in vim

I see

String iki = "brescia calcio‎<200e>";

      



I bet the mystery will be solved after removing this symbol.

If you're curious, U + 200E is left to right .

+11


source







All Articles