In my Java program, I have two strings s1
and s2
, when they are printed they both look equal, however, because they are encoded differently s1.equals(s2)
returns false. How would I compare these two strings so that even if they are encoded differently they would still be equal?
Look at this example code:
s1 = s1.trim();
s2 = s2.trim();
byte[] s1bytes = s1.getBytes();
byte[] s2bytes = s2.getBytes();
System.out.println(s1+","+s2+","+s1.equals(s2));
System.out.println("\ns1's bytes are:");
for (int i = 0; i < s1bytes.length; i++) {
System.out.println(s1bytes[i]);
}
System.out.println("\ns2's bytes are:");
for (int i = 0; i < s2bytes.length; i++) {
System.out.println(s2bytes[i]);
}
This prints:
SHEOGMIOF,SHEOGMIOF,false
s1's bytes are:
-17
-69
-65
83
72
69
79
71
77
73
79
70
s2's bytes are:
83
72
69
79
71
77
73
79
70
As you can see when printed s1
and s2
look the same, when compared they are are not equal and both of their byte arrays are different.
EDIT: My question is different from this question because I am not reading data in from a file, the source code in the .java file is encoded differently not the data from another file.