We often have to check if a string contains a substring or not in a case-insensitive manner. There are multiple ways to do so. In this article, we will take a look at these methods for java String contains Ignore Case checks.
Next, we will look at each method in detail.
Table of Contents
Using String.toLowerCase()
One of the easiest ways to check if a String has a substring without considering the case is to convert all the Strings to lowercase and then check for a substring.
To check for a substring, we use the contains() method, and for converting the String to lowercase, we use the toLowerCase() method.
1 2 3 4 5 6 |
String str1 = "Life is like riding a bicycle. To keep your balance, you must keep moving. ALBERT EINSTEIN"; boolean res1 = str1.toLowerCase().contains("albert".toLowerCase()); boolean res2 = str1.toLowerCase().contains("ALBERT".toLowerCase()); |
The above two examples give the output "true" since Albert is present in the String.
Result of toLowerCase: true
Using Pattern.compile()
With the Pattern.compile()
method, we can convert the pattern we pass to an insensitive regex pattern. Using the regex, we can use a Matcher
to find if one String contains the other.
For example,
1 2 3 4 |
boolean res3 = Pattern.compile(Pattern.quote("AlBerT"),Pattern.CASE_INSENSITIVE).matcher(str1).find(); System.out.println("Result is: " + res3); |
The result is true as long as the characters we are trying to match in our String are present in the target string.
Using String’s Matches
The String.matches()
method is using a regex pattern to find matching strings. If we use a (?i) argument in the matches method, we make it case-insensitive.
For example,
1 2 3 4 |
boolean res4 = str1.matches("(?i).*" + "AlBerT" + ".*"); System.out.println("Result of the matches operation is: " + res4); |
The output is again true because we check if Albert exists in the String in a case-insensitive manner.
IF we do the same operation but do not pass the (?i) parameter, we will get False.
1 2 3 4 |
boolean res6 = str1.matches("AlBerT" + ".*"); System.out.println("Result of the matches operation is: " + res6); |
Using StringUtils.containsIgnoreCase()
The apache foundation provides the apache-commons-lang
jar. This jar contains a collection of utility classes, one of which is the StringUtils class. The StringUtils
class contains a containsIgnoreCase()
method that checks if a string is a substring of another in a case-insensitive manner.
Before we can use the methods in this jar, we need to add it our Build Path.
Here is the dependency which you need to add for Apache common lang3 in pom.xml
.
1 2 3 4 5 6 7 |
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency> |
Once added, we can use it as follows:
1 2 3 4 |
boolean res5 = StringUtils.containsIgnoreCase(str1, "RiDing"); System.out.println("Result of StringUtils: " + res5); |
Complete program for Java String contains IgnoreCase
Here is complete program with all above examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package org.arpit.java2blog; import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; public class MainApplication { public static void main(String[] args) { String str1 = "Life is like riding a bicycle. To keep your balance, you must keep moving. ALBERT EINSTEIN"; boolean res1 = str1.toLowerCase().contains("albert".toLowerCase()); boolean res2 = str1.toLowerCase().contains("ALBERT".toLowerCase()); //First method System.out.println("Result of toLowerCase: " + res1); System.out.println("Result of toLowerCase: " + res2); //Pattern.compile boolean res3 = Pattern.compile(Pattern.quote("AlBerT"), Pattern.CASE_INSENSITIVE).matcher(str1).find(); System.out.println("Result is: " + res3); //String.matches boolean res4 = str1.matches("(?i).*" + "AlBerT" + ".*"); System.out.println("Result of the matches operation is: " + res4); boolean res6 = str1.matches("AlBerT" + ".*"); System.out.println("Result of the matches operation is: " + res6); boolean res5 = StringUtils.containsIgnoreCase(str1, "RiDing"); System.out.println("Result of StringUtils: " + res5); } } |
Output:
Result of toLowerCase: true
Result is: true
Result of the matches operation is: true
Result of the matches operation is: false
Result of StringUtils: true
Conclusion
In this article, we saw multiple ways to check if a string is a substring of another in a case-insensitive manner.
That’s all about Java String contains ignore case.