0 0 Votes

Java: Case Insensitive Equals - String Comparison

Info by Anja Proggy | 08/12/2013 at 19:22

Previously, when comparing two Strings in Java and the comparison should be executed case insensitive independent from the uppercase or lowercase writing of the String, I have turned the whole String in upper or lower letters before the comparison:

String s1 = "test";
String s2 = "Test";

if (s1.toUpperCase().equals(s2.toUpperCase())) {
  
}

In this example, both "test" and "Test" become "TEST" and the result of the comparison will be true.

But not long ago, I experienced that it can be so simple:

String s1 = "test";
String s2 = "Test";

if (s1.equalsIgnoreCase(s2)) {

}

With the function .equalsIgnoreCase() Java is providing a method fitting perfect for this scenario making your code simple and easier to read. Especially in long comparisons, this is a good way to use.

ReplyPositiveNegative

About the Author

Avatar AuthorThe author has not added a short description to his profile yet.
Show Profile | Message

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own articles on askingbox.com. How to do.