Java Check two ASCII Strings Equals Ignore Case using Google Guava
Tags: Google Guava Ascii String
In this Java tutorial we learn how to use the com.google.common.base.Ascii class of Google Guava to indicate whether the contents of two String are equal, ignoring lowercase or uppercase.
How to add Google Guava library to the Java project
To use the Google Guava library in the Gradle build project, add the following dependency into the build.gradle file.
implementation group: 'com.google.guava', name: 'guava', version: '30.1.1-jre'
To use the Google Guava library in the Maven build project, add the following dependency into the pom.xml file.
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
</dependency>
To have more information about the Google Guava library you can visit the project home page at guava.dev
How to check two Strings are equal ignoring case in Java
In the following Java example program we learn how to use the Ascii.equalsIgnoreCase() method to check whether two given String values are equal, ignoring lowercase or uppercase.
AsciiEqualsIgnoreCaseExample.java
import com.google.common.base.Ascii;
public class AsciiEqualsIgnoreCaseExample {
public static void main(String... args) {
boolean result1 = Ascii.equalsIgnoreCase("SIMPLE", "simple");
boolean result2 = Ascii.equalsIgnoreCase("solution", "solution");
boolean result3 = Ascii.equalsIgnoreCase("simple", "solution");
System.out.println("\"SIMPLE\" equalsIgnoreCase \"simple\": " + result1);
System.out.println("\"solution\" equalsIgnoreCase \"solution\": " + result2);
System.out.println("\"simple\" equalsIgnoreCase \"solution\": " + result3);
}
}
"SIMPLE" equalsIgnoreCase "simple": true
"solution" equalsIgnoreCase "solution": true
"simple" equalsIgnoreCase "solution": false
Happy Coding 😊