GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Java String contains() Method with Example - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Java String contains() Method with Example

Last Updated : 11 Jul, 2025

The String.contains() method is used to search for a sequence of characters within a string. In this article, we will learn how to effectively use the string contains functionality in Java.

Example:

In this example, we check if a specific substring is present in the given string.

Java
// Java Program to demonstrate working contains() method
class Gfg {

    public static void main(String args[]) {
      
        String s = "My name is GFG";

        System.out.println(s.contains("GFG"));
        System.out.println(s.contains("geeks"));
    }
}

Output
true
false

Table of Content

  • Syntax of contains() method
  • Examples of String.contains() Method
    • Case-Sensitive Check Using contains() Method
    • Null Handling Using contains() Method
    • Using StringBuilder

Syntax of contains() method

public boolean contains(CharSequence sequence);

Parameter:

  • sequence: This is the sequence of characters to be searched.

Return Value:

  • Returns true, if the sequence is found.
  • Returns false, if not found.

Implementation of contains() method

public boolean contains(CharSequence sequence)
{
   return indexOf(sequence.toString()) > -1;
}

Here, the contains() method internally converts the CharSequence to a String and uses the indexOf() method to check for the substring. If indexOf() returns 0 or a positive value, contains() returns true; otherwise, it returns false.

Points to Remember:

  • Case-Sensitive: The contains() method is case-sensitive, so "Java" and "java" are treated differently. because, first "J" is in upper case and the second "j" is in lower case.
  • Null Handling: Passing null as a parameter will throw a NullPointerException.
  • Supports CharSequence: It works with String, StringBuilder, and StringBuffer.

Examples of String.contains() Method

Now, we will explore different examples to understand how the contains() method works in Java.

Case-Sensitive Check Using contains() Method

Here, we demonstrate that the contains() method is case-sensitive when searching for substrings.

Java
// Java Program to demonstrate case sensitivity of contains() method
class Gfg1 {

    public static void main(String args[])
    {
        String s = "Welcome! to GFG";

        System.out.println(s.contains("Gfg"));
        System.out.println(s.contains("GFG"));
    }
}

Output
false
true


Null Handling Using contains() Method

This approach is used to safely handle scenarios, where a null value might be passed to the contains() method, preventing unexpected runtime exceptions.

Java
// Java program to handle null 
// using contains() method of String class
public class NullHandling {
  
    public static void main(String[] args) {
      
        String s = "Null check";

        try {
            System.out.println(s.contains(null)); // This will throw an exception
        } catch (NullPointerException e) {
            System.out.println("NullPointerException caught: Substring cannot be null.");
        }
    }
}

Output
NullPointerException caught: Substring cannot be null.


Using StringBuilder

This approach is used when we need to check if a String contains content from a StringBuilder.

Java
// Java program to check if a String 
// contains a substring constructed using StringBuilder
public class StringBuilderExample {
  
    public static void main(String[] args) {
      
        String s1 = "Welcome to GFG";
        StringBuilder s2 = new StringBuilder("GFG");

        // Check if mainString contains substring
        System.out.println(s1.contains(s2)); // true
    }
}

Output
true


Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.