Java String contains Ignore Case

Java String contains IgnoreCase

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.

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.

The above two examples give the output "true" since Albert is present in the String.

Result of toLowerCase: true
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,

The result is true as long as the characters we are trying to match in our String are present in the target string.

Result is: true

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,

The output is again true because we check if Albert exists in the String in a case-insensitive manner.

Result of the matches operation is: true

IF we do the same operation but do not pass the (?i) parameter, we will get False.

Result of the matches operation is: false

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.

Once added, we can use it as follows:

Result of StringUtils: true

Complete program for Java String contains IgnoreCase

Here is complete program with all above examples:

Output:

Result of toLowerCase: true
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.

Was this post helpful?

Related Posts

  • 15 September

    How to Replace Space with Underscore in Java

    Table of ContentsReplace space with underscore in java1. Using replace() method2. Using replaceAll() method Learn about how to replace space with underscore in java. Replace space with underscore in java 1. Using replace() method Use String’s replace() method to replace space with underscore in java. String’s replace() method returns a string replacing all the CharSequence […]

  • 15 September

    How to Replace Comma with Space in Java

    Table of ContentsReplace comma with space in java1. Using replace() method2. Using replaceAll() method Learn about how to replace comma with space in java. Replace comma with space in java 1. Using replace() method Use String’s replace() method to replace comma with space in java. Here is syntax of replace() method: [crayon-632f207a30b6e579353063/] [crayon-632f207a30b77406927309/] Output: 1 […]

  • 11 June

    Remove Parentheses From String in Java

    Table of ContentsJava StringsRemove Parentheses From a String Using the replaceAll() MethodRemove Parentheses From a String by TraversingConclusion Java uses the Strings data structure to store the text data. This article discusses methods to remove parentheses from a String in Java. Java Strings Java Strings is a class that stores the text data at contiguous […]

  • Escape percent sign in java
    08 February

    Escape percent sign in String’s format method in java

    Table of ContentsEscape Percent Sign in String’s Format Method in JavaEscape Percent Sign in printf() Method in Java In this post, we will see how to escape Percent sign in String’s format() method in java. Escape Percent Sign in String’s Format Method in Java String’s format() method uses percent sign(%) as prefix of format specifier. […]

  • Find character in String in java
    02 February

    Find Character in String in Java

    Table of ContentsHow to Find Character in String in Java1. Using indexOf() Method to Find Character in String in Java2. Using lastIndexOf() Method to Find Char in String in JavaFind Character at Index in String in Javahow Do I Find Word in String in Java? In this post, we will see how to find character […]

  • 13 July

    Remove substring from String in Java

    Table of ContentsUsing String’s replace method to remove substring from String in JavaUsing String’s replaceFirst method to remove substring from String in JavaUsing String’s replaceAll method to remove substring from String in JavaUsing StringBuilder’s delete() method to remove substring from String in JavaUsing StringBuilder’s replace() method to remove substring from String in JavaConclusion In this […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.