-3

This is extremely odd. I am simply comparing two strings, which are the same, but they aren't comparing properly. text is a string I extract from a user conversation (it is trimmed):

String compareThis = sharedPrefs.getString("key", "default").toLowerCase().trim();    
if (text.equals(compareThis)){
    Toast.makeText(lol, "Good, strings matched :)", Toast.LENGTH_SHORT).show();
}else{
    Toast.makeText(context, "Should be " +text +" Not " +compareThis, Toast.LENGTH_SHORT).show();
}

Now look, this code looks simple enough, but the conditional is going into the else even though the strings are equal...Now you're thinking thats because the strings aren't equal. No, because I get this toast:

Should be hello Not hello

WHAT?! Those are the same string?! The toast even shows that those are the same...Why is the comparison not working if they are both "hello"?

Thanks,

Ruchir

Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83

1 Answers1

1

This is extremely odd. I am simply comparing two strings, which are the same, but they aren't..

are you sure they are the same strings ???

try get bytes and compare them (by bytes) or use codepoints

  try {
         byte[] bytesStr1 = Str1.getBytes("UTF-8");
         byte[] bytesStr2= Str2.getBytes("UTF-8");

        if(bytesStr1.length!=bytesStr.length)log("no match = size");

        for(int b=0;b<bytesStr1.length;b++) {
             if(bytesStr1[b] != bytesStr2[b]) 
                   return log("no match at pos: "+ b);
        }
  } catch (UnsupportedEncodingException e) {
       // handle exception here 
  }

there could be many reasons why the string comparison fails - for example

  • space vs. non breaking space <- trim will not help you here

simple example:

 String oldString = "\uD800";
 String newString = new String(oldString);
 String newString = new String(oldString.getBytes("UTF-8"), "UTF-8");
 newString.equals(oldString) // ???? false 

small appeal:

most of your(by this i mean most people here) problems can be solved by reading and understanding the basics :) - but instead you are all "drowning" from big "water"

Okay, I tried what you said and went byte by byte, but they were all the same. Also, the lengths were the same.. – Ruchir Baronia

so this mean that both strings "in your definition" are equals

instead of compare method you can use :

int pos =  String.indexOf(String);  //this will return you the firs position where the two strings don't match 
  • method implementation is similar to method which i wrote on the beginning of this post

lecture:

https://docs.oracle.com/javase/tutorial/java/data/comparestrings.html

now when we we code for android I recommend to take special attention - because google is rewriting most of java classes by itself String class from Oracle is not the same as Google String class because they don't run straight on ORACLE JVM !!! - see ART (Android Runtime) - every time when i visit google & browse repos i see code changes in google api & source code of android i see much more things rewrite in pure c (as native) - google wants to break with proprietary oracle java thus is using more of open jdk implementations

ceph3us
  • 7,326
  • 3
  • 36
  • 43
  • 2
    For now your answer doesn't look like answer, but more like asking for clarification with advice how to see potential problem. To make it into proper answer you could start it with something like "*If `equals` returns `false` then it means that compared strings are not equal, even if they look equal. There can be many reasons for that. Most of the time problem is that one of strings contain unprintable characters, or you is surrounded by whitespaces (so you would need to trim them first). To see what is the case you can use this code to compare their bytes*" – Pshemo Jun 18 '16 at 01:09
  • @Pshemo -i'ts nice that you are trying to teach an academic teacher? – ceph3us Jun 18 '16 at 01:13
  • @ceph3us It is not only nice, but lovely. – RaminS Jun 18 '16 at 01:14
  • @Pshemo - good to know there still are people willing to teach :) – ceph3us Jun 18 '16 at 01:17
  • Most of the time I am here to learn, but when I can see possibility for improvement I like to point it. Hope you don't mind :) – Pshemo Jun 18 '16 at 01:19
  • BTW empty `catch` is 99.99% of the time a bad thing, even (or maybe especially) in example. Could you add some line like `//handle exception` or `e.printStackTrace()` there? – Pshemo Jun 18 '16 at 01:21
  • @Pshemo - i don't - i like to start to write and let to evolve my answer in time – ceph3us Jun 18 '16 at 01:21
  • @Pshemo - in the subject of exceptions see http://stackoverflow.com/questions/10332132/how-to-use-null-in-switch/34843946#34843946 – ceph3us Jun 18 '16 at 01:24
  • Okay, I tried what you said and went byte by byte, but they were all the same. Also, the lengths were the same.. – Ruchir Baronia Jun 18 '16 at 01:27
  • I read your linked answer but I don't really see connection with my comment about empty catch block. I agree that exceptions can be very useful, but IMO we should always add some safety line like `e.printStackTrace()` instead of nothing. Even when we are sure that exception will not be thrown (because `UTF-8` is proper encoding name) there is still a chance that someday, someone will want to play around with our code and change that name into something which may not be recognized by Java and exception will be thrown. Anyway we can get rid of exceptions with `.getBytes(StandardCharsets.UTF_8)`. – Pshemo Jun 18 '16 at 01:57
  • Also I didn't downvote your linked answer (can easily prove it by temporary voting on it as you like - of course with assumption that I have only one account). – Pshemo Jun 18 '16 at 01:59
  • @Pshemo - i don't care if u vote up/down or not - here i look only for solution to my problems - but also sometimes i like to write my few words on some issues – ceph3us Jun 18 '16 at 09:08
  • I tried what you said and went byte by byte, but they were all the same. Also, the lengths were the same..What do you suggest next? – Ruchir Baronia Jun 18 '16 at 17:05
  • @RuchirBaronia - we checked that they are equals ( this method is simple comparator you can use it as own version for equal ) - if u want to debug your code push it to git and give me link i'll se why u don't get equality in your case – ceph3us Jun 18 '16 at 17:15