tony9099
tony9099

Reputation: 4717

String1.equalsIgnoreCase(String2) returns false even though both have same value

In my app, I am reading from HTTP page and converting the Stream via the below method into String.

public static String convertStreamToString(java.io.InputStream is)
{
    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); 
    return s.hasNext() ? s.next() : "";
}

However, once I have the String, I am comparing to another String (manually created in eclipse) which has the exact same component, (which is a copy paste from the same HTML file on my server). The comparison is returning me false.

I am using s1.equalsIgnoreCase(s2). Below is an image how my 2 strings are exactly alike.

Identical Strings

Upvotes: 1

Views: 695

Answers (1)

Raghav Sood
Raghav Sood

Reputation: 82553

Most likely, one of your Strings has some whitespace that results in them being unequal upon comparison.

Calling .trim() on the Strings before you compare them should fix this.

Upvotes: 1

Related Questions