
- 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
How do we check if a String contains a substring (ignoring case) in Java?
The contains() method of the String class accepts Sting value as a parameter, verifies whether the current String object contains the specified String and returns true if it does (else false).
The toLoweCase() method of the String class converts all the characters in the current String into lower case and returns.
To find whether a String contains a particular sub string irrespective of case −
Get the String.
Get the Sub String.
Convert the string value into lower case letters using the toLowerCase() method, store it as fileContents.
Convert the string value into lower case letters using the toLowerCase() method, store it as subString.
Invoke the contains() method on the fileContents by passing subString as a parameter to it.
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.
Following Java example reads a sub string from the user an verifies whether the file contains the given sub string irrespective of case.
import java.io.File; import java.util.Scanner; public class SubStringExample { 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"); //Converting the contents of the file to lower case fileContents = fileContents.toLowerCase(); //Converting the sub string to lower case subString = subString.toLowerCase(); //Verify whether the file contains the given sub String boolean result = fileContents.contains(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
- Method to check if a String contains a sub string ignoring case in Java
- 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
- Check if a String Contains a Substring in Linux
- How to check if a string contains a substring in Golang?
- How to check if a String contains another String in a case insensitive manner in Java?
- Golang program to check if a string contains a substring
- How to check if an input string contains a specified substring in JSP?
- How to check whether a string contains a substring in JavaScript?
- How to check whether a string contains a substring in jQuery?
- How do we check in Python whether a string contains only numbers?
- PHP 8 – Using str_contains() 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?
- How to check whether a String contains a substring or not?
