W
www
Hi,
I always thought I can use "==" to compare if two Strings are equal(with
same case too). I know there is a method .equals(Object o) or
equalsIgnoreCase(String s). But I always thought "==" is good enough.
This morning, I found out that I was wrong.
I have two Strings, str1 and str2. They received values from some where
else. When I print them out to screen, they look same.(I am aware of
some unvisible thing, so I used trim()
//str1 and str2
if(str1.trim() == str2.trim())
{
System.out.println("equal");
}
else
{
System.out.println("Not equal");
}
Above code print out "Not equal". But the following code print out "equal".
if(str1.trim().equals(str2.trim()))
{
System.out.println("equal");
}
else
{
System.out.println("Not equal");
}
Can you give me some hint? Thank you.
I always thought I can use "==" to compare if two Strings are equal(with
same case too). I know there is a method .equals(Object o) or
equalsIgnoreCase(String s). But I always thought "==" is good enough.
This morning, I found out that I was wrong.
I have two Strings, str1 and str2. They received values from some where
else. When I print them out to screen, they look same.(I am aware of
some unvisible thing, so I used trim()
//str1 and str2
if(str1.trim() == str2.trim())
{
System.out.println("equal");
}
else
{
System.out.println("Not equal");
}
Above code print out "Not equal". But the following code print out "equal".
if(str1.trim().equals(str2.trim()))
{
System.out.println("equal");
}
else
{
System.out.println("Not equal");
}
Can you give me some hint? Thank you.