04 November 2008

Java String Comparison

One little gotcha in Java is the string comparisons: Using the equality check == will tell you if the two variables are referencing the same object, not whether their string values are the same. To do a string comparison, use something such as this,

 if (string1.equalsIgnoreCase(string2)) {
//... do something

Otherwise you'll be wondering why your code isn't working ;)

No comments: