Java String contains() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Java, contains() checks whether a character sequence appears as a contiguous substring. It returns true or false.

You pass the text to find. It is required. The search is case-sensitive. Need the index as well? Use indexOf().

Syntax

Call contains on the string you search in.

java
boolean ok = text.contains("Java");
CallWhat it does
text.contains("Java")Returns true when Java appears anywhere in text.

The parameter type is CharSequence, so String and StringBuilder both work.

Library declarations
java
public boolean contains(CharSequence sequence);

Parameters

ParameterDescription
sequenceRequired. The substring to find. Do not pass null.

Return Value

Return ValueDescription
trueThe substring was found.
falseThe substring was not found.

Java Examples

Example 1 — Basic Substring Check

See true and false when a sequence is present, missing, or differs only by case.

java
public class ContainsBasic {
    public static void main(String[] args) {
        String text = "CodeToFun teaches Java";

        System.out.println(text.contains("Java"));
        System.out.println(text.contains("Python"));
        System.out.println(text.contains("code"));
    }
}

How It Works

  1. First call. "Java" appears → true.
  2. Second call. "Python" is absent → false.
  3. Third call. "code" fails because the real text starts with uppercase C.
  4. Exact match. Matching is contiguous and case-sensitive.

Example 2 — Case-Insensitive Search

Normalize both sides with toLowerCase() before calling contains.

java
public class ContainsIgnoreCase {
    public static void main(String[] args) {
        String text = "Welcome to JAVA";
        String needle = "java";

        boolean found = text.toLowerCase().contains(needle.toLowerCase());
        System.out.println(found);
    }
}

How It Works

  1. Default fails. Plain contains("java") would be false.
  2. Lower both sides. Normalize text and needle first.
  3. Then search. Call contains on the normalized string.
  4. Same letters match. Result is true ignoring case.

Example 3 — Simple Input Validation

Use contains for quick shape checks (beginner-level only).

java
public class ContainsValidate {
    public static void main(String[] args) {
        String email = "[email protected]";

        if (email.contains("@") && email.contains(".")) {
            System.out.println("Looks like an email shape");
        } else {
            System.out.println("Missing @ or .");
        }
    }
}

How It Works

  1. Check for @. Require the at-sign somewhere in the text.
  2. Check for a dot. Also require ..
  3. Both pass. This sample address satisfies both.
  4. Beginner check only. Real email validation needs stronger rules.

Frequently Asked Questions

contains() checks whether a substring appears in a string and returns true or false.
sequence is required. It is the text to find.
Yes. Convert both sides with toLowerCase() for an ignore-case check.
contains() returns boolean. indexOf() returns the starting index or -1.
Every string contains the empty sequence, so the result is true.

Next: contentEquals()

Compare a string’s character content with another CharSequence.

contentEquals() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful