
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency>
Get the source String.
Get the search String.
Invoke the containsIgnoreCase() method by passing above two string objects as parameters (in the same order).
Example
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.
Example
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.io.File; 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(); sb.append(input); } return sb.toString(); } public static void main(String args[]) throws Exception { Scanner sc = new Scanner(System.in); System.out.println("Enter the sub string to be verified: "); String subString = sc.next(); 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."); } } }
Output
Enter the sub string to be verified: comforts of their drawing rooms. File contains the given sub string.
- Related Articles
- How do we check if a String contains a substring (ignoring case) in Java?
- Check if a string contains a sub-string in C++
- How to check if a string contains a specific sub string?
- How to check if a String contains another String in a case insensitive manner in Java?
- Check if a string contains a palindromic sub-string of even length in Python
- Check if a string contains a palindromic sub-string of even length in C++
- How to check whether an input string contains a specified substring ignoring the case using JSP?
- Java Program to Check if a string contains a substring
- How to check if a string contains only lower case letters in Python?
- How to check if a string contains only upper case letters in Python?
- Check if a string contains a number using Java.
- Java Regular expression to check if a string contains alphabet
- How to test if a Java String contains a case insensitive regex pattern
- Java program to check if a string contains any special character
- Check if a string contains only alphabets in Java using Regex
