---
description: What is contains()method in Java? The contains() method is Java method to check if String contains another substring or not. It returns boolean value so it can use directly inside if statements. Synt
title: Java String contains(): Check if String contains Substring
image: https://www.guru99.com/images/java-string-contains.png
---

 

[Skip to content](#main) 

**⚡ Smart Summary**

Java String contains() method checks whether a specified sequence of characters exists within a given string, returning a boolean true or false. It is case-sensitive, accepts a CharSequence argument, and is commonly used inside if statements for substring testing.

* 🔎 **Purpose:** Checks whether a specific set of characters is part of a given string.
* ✅ **Return Value:** Returns boolean true if the sequence is a substring, otherwise false.
* 🔡 **Case-Sensitive:** Treats uppercase and lowercase differently, so “Example” and “example” do not match.
* ⚠️ **Exception:** Throws a NullPointerException when the argument passed is null.
* 🔁 **Common Use:** Frequently used inside if-else statements to test for a substring.

[ Read More ](javascript:void%280%29;) 

![Java String contains\(\) Method]()

## Java String contains() method

The **Java String contains()** method is used to check whether the specific set of characters are part of the given string or not. It returns a boolean value true if the specified characters are substring of a given string and returns false otherwise. It can be directly used inside the if statement.

## Syntax of contains() method in Java

public boolean String.contains(CharSequence s)

#### Parameters

s − This is the sequence to search in Java contains() method

#### Return Value

The contains() method in Java returns true only if this string contains “s” else false.

#### Exception

NullPointerException − if the value of s is null in the Java contains() method.

## When to use Contains() method?

contains() in Java is a common case in [Java programming](https://www.guru99.com/java-tutorial.html) when you want to check if specific String contains a particular substring. For example, If you want to test if the String “The big red fox” contains the substring “red.” The string contains() in Java method is useful in such situation.

### Java String contains() Method Example 1:

public class Sample_String {
    public static void main(String[] args) {
        String str_Sample = "This is a String contains Example";
        //Check if String contains a sequence
        System.out.println("Contains sequence 'ing': " + str_Sample.contains("ing"));
        System.out.println("Contains sequence 'Example': " + str_Sample.contains("Example"));
        //String contains method is case sensitive
        System.out.println("Contains sequence 'example': " + str_Sample.contains("example"));
        System.out.println("Contains sequence 'is String': " + str_Sample.contains("is String"));
    }
}

**Expected Output**:

Contains sequence 'ing': true
Contains sequence 'Example': true
Contains sequence 'example': false
Contains sequence 'is String': false

### RELATED ARTICLES

* [Encapsulation in Java ](https://www.guru99.com/encapsulation-in-java.html "Encapsulation in Java")
* [How to Create Packages in Java ](https://www.guru99.com/packages-in-java.html "How to Create Packages in Java")
* [For-each loop in Java ](https://www.guru99.com/foreach-loop-java.html "For-each loop in Java")
* [How to Reverse a String in Java using Recursion ](https://www.guru99.com/java-program-reverse-string.html "How to Reverse a String in Java using Recursion")

### Java String contains() Method Example 2:

Java String contains() method in the if else Structure:

public class IfExample {
    public static void main(String args[]) {
        String str1 = "Java string contains If else Example";
        // In If-else statements you can use the contains() method

        if (str1.contains("example")) {
            System.out.println("The Keyword :example: is found in given string");
        } else {
            System.out.println("The Keyword :example: is not found in the string");
        }
    }
}

**Expected Output:**

The Keyword :example: is not found in the string 

This code is editable. Click Run to Compile + Execute   

![]()

## FAQs

🔡 Is the Java String contains() method case-sensitive?

Yes. The Java String contains() method is case-sensitive. For example, “String”.contains(“string”) returns false because the capitalization differs. Convert both strings to the same case for case-insensitive checks.

✅ What does the contains() method return in Java?

The contains() method returns a boolean value: true if the specified CharSequence is found within the string, and false otherwise. It throws a NullPointerException if the argument is null.

⚖️ What is the difference between contains() and indexOf()?

contains() returns a boolean indicating whether a substring exists, while indexOf() returns the integer position of the substring or -1 if not found. Use indexOf() when you need the location.

🤖 How can AI help with Java string methods?

AI assistants can suggest the right string method, generate examples, explain case sensitivity, and detect bugs such as missing null checks. They help beginners use methods like contains() correctly.

🧠 Can AI write Java string-search code automatically?

Yes. AI tools can generate Java code that searches strings using contains(), indexOf(), or regular expressions, and explain how each approach works for different matching needs.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/java-string-contains.png","url":"https://www.guru99.com/images/java-string-contains.png","width":"700","height":"250","caption":"Java String contains()","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/string-contains-method-java.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/java-tutorials","name":"Java Tutorials"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/string-contains-method-java.html","name":"Java String contains(): Check if String contains Substring"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/string-contains-method-java.html#webpage","url":"https://www.guru99.com/string-contains-method-java.html","name":"Java String contains(): Check if String contains Substring","dateModified":"2026-06-30T16:52:24+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/java-string-contains.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/string-contains-method-java.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/james","name":"James Hartman","description":"I am James Hartman, a seasoned professional in Oracle Certified Java Professional tutorials, specializing in crafting comprehensive guides to help you excel in your Java certification journey.","url":"https://www.guru99.com/author/james","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/james-hartman-author-v2-120x120.png","url":"https://www.guru99.com/images/james-hartman-author-v2-120x120.png","caption":"James Hartman","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Java Tutorials","headline":"Java String contains(): Check if String contains Substring","description":"What is contains()method in Java? The contains() method is Java method to check if String contains another substring or not. It returns boolean value so it can use directly inside if statements. Synt","keywords":"java","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/james","name":"James Hartman"},"dateModified":"2026-06-30T16:52:24+05:30","image":{"@id":"https://www.guru99.com/images/java-string-contains.png"},"copyrightYear":"2026","name":"Java String contains(): Check if String contains Substring","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"Is the Java String contains() method case-sensitive?","acceptedAnswer":{"@type":"Answer","text":"Yes. The Java String contains() method is case-sensitive. For example, \"String\".contains(\"string\") returns false because the capitalization differs. Convert both strings to the same case for case-insensitive checks."}},{"@type":"Question","name":"What does the contains() method return in Java?","acceptedAnswer":{"@type":"Answer","text":"The contains() method returns a boolean value: true if the specified CharSequence is found within the string, and false otherwise. It throws a NullPointerException if the argument is null."}},{"@type":"Question","name":"What is the difference between contains() and indexOf()?","acceptedAnswer":{"@type":"Answer","text":"contains() returns a boolean indicating whether a substring exists, while indexOf() returns the integer position of the substring or -1 if not found. Use indexOf() when you need the location."}},{"@type":"Question","name":"How can AI help with Java string methods?","acceptedAnswer":{"@type":"Answer","text":"AI assistants can suggest the right string method, generate examples, explain case sensitivity, and detect bugs such as missing null checks. They help beginners use methods like contains() correctly."}},{"@type":"Question","name":"Can AI write Java string-search code automatically?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI tools can generate Java code that searches strings using contains(), indexOf(), or regular expressions, and explain how each approach works for different matching needs."}}]}],"@id":"https://www.guru99.com/string-contains-method-java.html#schema-1126823","isPartOf":{"@id":"https://www.guru99.com/string-contains-method-java.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/string-contains-method-java.html#webpage"}}]}
```
