Java: Find Substring in String

Finding a substring within a string is a common task in Java. This guide will cover different ways to find a substring, including using the indexOf method, the contains method, and regular expressions.

Table of Contents

  1. Introduction
  2. Using indexOf Method
  3. Using contains Method
  4. Using Regular Expressions
  5. Conclusion

Introduction

In Java, strings are objects that represent sequences of characters. Finding a substring within a string involves checking if a particular sequence of characters exists within another sequence. This can be done using various methods, each suited to different scenarios.

Using indexOf Method

The indexOf method of the String class can be used to find the starting index of the first occurrence of a substring.

Example

public class FindSubstringExample {
    public static void main(String[] args) {
        String str = "Hello, welcome to the world of Java!";
        String substr = "welcome";

        int index = str.indexOf(substr);

        if (index != -1) {
            System.out.println("Substring \"" + substr + "\" found at index: " + index);
        } else {
            System.out.println("Substring \"" + substr + "\" not found.");
        }
    }
}

Explanation

  • The indexOf method is called on the string str with the substring substr as the argument.
  • If the substring is found, indexOf returns the starting index of the first occurrence of the substring.
  • If the substring is not found, indexOf returns -1.

Output:

Substring "welcome" found at index: 7

Using contains Method

The contains method of the String class checks if a sequence of characters is present in the string.

Example

public class FindSubstringExample {
    public static void main(String[] args) {
        String str = "Hello, welcome to the world of Java!";
        String substr = "welcome";

        boolean found = str.contains(substr);

        if (found) {
            System.out.println("Substring \"" + substr + "\" is found.");
        } else {
            System.out.println("Substring \"" + substr + "\" is not found.");
        }
    }
}

Explanation

  • The contains method is called on the string str with the substring substr as the argument.
  • If the substring is found, contains returns true.
  • If the substring is not found, contains returns false.

Output:

Substring "welcome" is found.

Using Regular Expressions

Regular expressions provide a powerful way to search for substrings based on patterns.

Example

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

public class FindSubstringExample {
    public static void main(String[] args) {
        String str = "Hello, welcome to the world of Java!";
        String substr = "welcome";

        Pattern pattern = Pattern.compile(substr);
        Matcher matcher = pattern.matcher(str);

        if (matcher.find()) {
            System.out.println("Substring \"" + substr + "\" found at index: " + matcher.start());
        } else {
            System.out.println("Substring \"" + substr + "\" not found.");
        }
    }
}

Explanation

  • A Pattern object is created using the compile method with the substring substr as the pattern.
  • A Matcher object is created by calling the matcher method on the pattern with the string str as the argument.
  • The find method of the Matcher class checks if the pattern is found in the string.
  • If the pattern is found, start returns the starting index of the match.

Output:

Substring "welcome" found at index: 7

Conclusion

Finding a substring within a string in Java can be accomplished using various methods, each with its own advantages. The indexOf method provides a straightforward approach to finding the starting index of a substring. The contains method offers a simple way to check for the presence of a substring. Regular expressions provide a powerful and flexible solution for more complex pattern matching. Depending on your specific use case and requirements, you can choose the method that best fits your needs.

Comments