View RSS Feed

Java String

Java String comparison with the compareTo method

Rate this Entry
by , 04-26-2012 at 07:02 PM (1291 Views)
Also a less common 3rd way for comparison of the Java Strings is present. That method is CompareTo method, with string class. In case of two similar Java Strings, this method will be returning a zero value. String Comparison approach shall look like this given example:

Java Code:
String string1 = "foo bar";
String string2 = "foo bar";

// java string compare example
if (string1.compareTo(string2) == 0)
{
  // this line WILL print
  System.out.println("The two strings are the same.")
}

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

Categories
Compare

Comments