Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this quick tutorial, we’ll look at determining if two String values are the same when we ignore case.

2. Using the equalsIgnoreCase()

equalsIgnoreCase() accepts another String and returns a boolean value:

String lower = "equals ignore case";
String UPPER = "EQUALS IGNORE CASE";

assertThat(lower.equalsIgnoreCase(UPPER)).isTrue();

3. Using Apache Commons Lang

The Apache Commons Lang library contains a class called StringUtils that provides a method similar to the method above, but it has the added benefit of handling null values:

String lower = "equals ignore case"; 
String UPPER = "EQUALS IGNORE CASE"; 

assertThat(StringUtils.equalsIgnoreCase(lower, UPPER)).isTrue();
assertThat(StringUtils.equalsIgnoreCase(lower, null)).isFalse();

4. Conclusion

In this article, we took a quick look at determining if two String values are the same when we ignore case. Now, things get a bit trickier when we internationalize, as case-sensitivity is specific to a language – stay tuned for more info.

And, as always, all code examples can be found over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.