Method to check if a String contains a sub string ignoring case in Java

The StringUtils class of org.apache.commons.lang3 package of Apache commons library provides a method named containsIgnoreCase().

This method accepts two String values representing a source string and search string respectively, verifies whether the source string contains the search string ignoring the case. It returns a boolean value which is −

  • true, if the source string contains the search string.

  • false, if the source string does not contain the search string.

To find whether a String contains a particular sub string irrespective of case −

  • Add the following dependency to your pom.xml file

  • Get the source String.

  • Get the search String.

  • Invoke the containsIgnoreCase() method by passing above two string objects as parameters (in the same order).


Assume we have a file named sample.txt in the D directory with the following content −

Tutorials point originated from the idea that there exists a class of readers who respond better to on-line content
and prefer to learn new skills at their own pace from the comforts of their drawing rooms.
At Tutorials point we provide high quality learning-aids for free of cost.


Following Java example reads a sub string from the user and verifies whether the file contains the given sub string irrespective of case.

import java.util.Scanner;
import org.apache.commons.lang3.StringUtils;
public class ContainsIgnoreCaseExample {
   public static String fileToString(String filePath) throws Exception {
      String input = null;
      Scanner sc = new Scanner(new File(filePath));
      StringBuffer sb = new StringBuffer();
      while (sc.hasNextLine()) {
         input = sc.nextLine();
      return sb.toString();
   public static void main(String args[]) throws Exception {
      Scanner sc = new Scanner(;
      System.out.println("Enter the sub string to be verified: ");
      String subString =;
      String fileContents = fileToString("D:\sample.txt");
      //Verify whether the file contains the given sub String
      boolean result = StringUtils.containsIgnoreCase(fileContents, subString);
      if(result) {
         System.out.println("File contains the given sub string.");
      }else {
         System.out.println("File doesnot contain the given sub string.");


Enter the sub string to be verified:
comforts of their drawing rooms.
File contains the given sub string.