View RSS Feed

Java String

Java String comparison with the equals method

Rate this Entry
by , 04-26-2012 at 07:00 PM (518 Views)
About 95% of time, I prefer comparing strings by using Java String class’ equal methods. It is shown below:
Java Code:
if (string1.equals(string2))
String equal method observes if exactly similar string of characters are present in Java strings, they would be considered as equal.
Quick String comparison could be observed, with equal method, 2 strings wouldn’t be equal as same characters are not present.
Java Code:
String string1 = "foo";
String string2 = "FOO";

if (string1.equals(string2))
{
  // this line will not print because the
  // java string equals method returns false:
  System.out.println("The two strings are the same.")
}
When exactly similar character’s string is present in two strings it means String equal method would be returned as true. For example:
Java Code:
String string1 = "foo";
String string2 = "foo";

// test for equality with the java string equals method
if (string1.equals(string2))
{
  // this line WILL print
  System.out.println("The two strings are the same.")
}

Submit "Java String comparison with the equals method" to Facebook Submit "Java String comparison with the equals method" to Digg Submit "Java String comparison with the equals method" to del.icio.us Submit "Java String comparison with the equals method" to StumbleUpon Submit "Java String comparison with the equals method" to Google

Categories
Compare

Comments