Java: Check If String Contains Substring

In Java, checking if a string contains a specific substring is a common task. This can be done using various methods, each suited to different scenarios. This guide will cover different ways to check if a string contains a substring, including using the contains method, indexOf method, matches method with regular expressions and Pattern and Matcher classes.

Table of Contents

  1. Introduction
  2. Using String.contains Method
  3. Using String.indexOf Method
  4. Using String.matches Method with Regular Expressions
  5. Using Pattern and Matcher Classes
  6. Conclusion

Introduction

Strings in Java are sequences of characters used to represent text. Checking if a string contains a specific substring can be done using various methods, each with its own advantages and use cases.

Using String.contains Method

The contains method checks if a string contains a specified sequence of characters.

Example

public class SubstringCheckExample {
    public static void main(String[] args) {
        String str = "Hello, world!";
        String substr1 = "world";
        String substr2 = "Java";

        System.out.println(containsSubstring(str, substr1)); // true
        System.out.println(containsSubstring(str, substr2)); // false
    }

    public static boolean containsSubstring(String str, String substr) {
        return str.contains(substr);
    }
}

Explanation

  • str.contains(substr): Returns true if str contains substr, false otherwise.

Using String.indexOf Method

The indexOf method returns the index within the string of the first occurrence of the specified substring, or -1 if the substring is not found.

Example

public class SubstringCheckExample {
    public static void main(String[] args) {
        String str = "Hello, world!";
        String substr1 = "world";
        String substr2 = "Java";

        System.out.println(containsSubstring(str, substr1)); // true
        System.out.println(containsSubstring(str, substr2)); // false
    }

    public static boolean containsSubstring(String str, String substr) {
        return str.indexOf(substr) != -1;
    }
}

Explanation

  • str.indexOf(substr) != -1: Returns true if str contains substr, false otherwise.

Using String.matches Method with Regular Expressions

The matches method can be used with regular expressions to check if a string contains a substring. However, this method is generally used for full string matching rather than partial matching.

Example

public class SubstringCheckExample {
    public static void main(String[] args) {
        String str = "Hello, world!";
        String substr1 = "world";
        String substr2 = "Java";

        System.out.println(containsSubstring(str, substr1)); // true
        System.out.println(containsSubstring(str, substr2)); // false
    }

    public static boolean containsSubstring(String str, String substr) {
        return str.matches(".*" + substr + ".*");
    }
}

Explanation

  • str.matches(".*" + substr + ".*"): Uses a regular expression to check if str contains substr. The .* before and after the substring allows for any characters to be present before or after the substring.

Using Pattern and Matcher Classes

The Pattern and Matcher classes from the java.util.regex package provide more flexibility and control when working with regular expressions.

Example

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SubstringCheckExample {
    public static void main(String[] args) {
        String str = "Hello, world!";
        String substr1 = "world";
        String substr2 = "Java";

        System.out.println(containsSubstring(str, substr1)); // true
        System.out.println(containsSubstring(str, substr2)); // false
    }

    public static boolean containsSubstring(String str, String substr) {
        Pattern pattern = Pattern.compile(Pattern.quote(substr));
        Matcher matcher = pattern.matcher(str);
        return matcher.find();
    }
}

Explanation

  • Pattern.compile(Pattern.quote(substr)): Compiles the regular expression. Pattern.quote is used to escape any special characters in the substring.
  • matcher.find(): Returns true if the substring is found in the string, false otherwise.

Conclusion

Checking if a string contains a substring in Java can be accomplished using various methods, including the contains method, indexOf method, matches method with regular expressions, and the Pattern and Matcher classes. Each method has its own advantages and specific use cases:

  • The contains method is straightforward and easy to use.
  • The indexOf method is useful if you also need the index of the substring.
  • The matches method with regular expressions provides a way to use regex for substring checks, though it's more commonly used for full string matching.
  • The Pattern and Matcher classes provide a flexible and powerful way to work with regular expressions, especially for complex patterns.

By understanding these methods, you can choose the most appropriate one for your specific use case when working with strings in Java.

Comments