Java String.contains() Method Example

The String.contains() method in Java is used to check if a string contains a specified sequence of characters. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. contains Method Syntax
  3. Examples
    • Basic Example
    • Case Sensitivity
    • Checking for a Substring in Different Positions
  4. Conclusion

Introduction

The String.contains() method is part of the String class in Java. It allows you to determine whether a given string contains a specified sequence of characters. This is useful in various scenarios, such as searching for keywords in text, validating input, or checking for the presence of certain substrings.

contains Method Syntax

The syntax for the contains method is as follows:

public boolean contains(CharSequence s)
  • s: The sequence of characters to be searched for.
  • Returns: true if the sequence of characters is found in the string; false otherwise.

Examples

Basic Example

In this example, we'll use the contains method to check if a string contains a specified sequence of characters.

Example

public class ContainsExample {
    public static void main(String[] args) {
        String str = "Hello, World!";

        // Check if the string contains "World"
        boolean result = str.contains("World");

        System.out.println("Does the string contain 'World'? " + result);
    }
}

Output:

Does the string contain 'World'? true

Case Sensitivity

The contains method is case-sensitive. This means that the method will return false if the case of the characters does not match.

Example

public class ContainsCaseSensitivityExample {
    public static void main(String[] args) {
        String str = "Hello, World!";

        // Check if the string contains "world"
        boolean result = str.contains("world");

        System.out.println("Does the string contain 'world'? " + result);
    }
}

Output:

Does the string contain 'world'? false

Checking for a Substring in Different Positions

You can use the contains method to check if a string contains a substring regardless of its position in the string.

Example

public class ContainsSubstringExample {
    public static void main(String[] args) {
        String str = "The quick brown fox jumps over the lazy dog";

        // Check if the string contains "quick"
        boolean containsQuick = str.contains("quick");

        // Check if the string contains "lazy"
        boolean containsLazy = str.contains("lazy");

        // Check if the string contains "cat"
        boolean containsCat = str.contains("cat");

        System.out.println("Does the string contain 'quick'? " + containsQuick);
        System.out.println("Does the string contain 'lazy'? " + containsLazy);
        System.out.println("Does the string contain 'cat'? " + containsCat);
    }
}

Output:

Does the string contain 'quick'? true
Does the string contain 'lazy'? true
Does the string contain 'cat'? false

Handling Null Sequence

If the sequence to be checked is null, the contains method will throw a NullPointerException.

Example

public class ContainsNullSequenceExample {
    public static void main(String[] args) {
        String str = "Hello, World!";

        try {
            // Check if the string contains null
            boolean result = str.contains(null);
        } catch (NullPointerException e) {
            System.out.println("NullPointerException: " + e.getMessage());
        }
    }
}

Output:

NullPointerException: Cannot invoke "CharSequence.length()" because "s" is null

Conclusion

The String.contains() method in Java provides a way to check if a string contains a specified sequence of characters. This method is case-sensitive and is useful for various string searching and validation scenarios. By understanding how to use this method, you can effectively perform substring checks in your Java applications.


Comments