String Substring Search in Java

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn how to find a word or substring in a Java String. We will use the indexOf() and contains() methods of the String class to locate the substring in the given String.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/if_else("`If...Else`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117435{{"`String Substring Search in Java`"}} java/classes_objects -.-> lab-117435{{"`String Substring Search in Java`"}} java/class_methods -.-> lab-117435{{"`String Substring Search in Java`"}} java/modifiers -.-> lab-117435{{"`String Substring Search in Java`"}} java/oop -.-> lab-117435{{"`String Substring Search in Java`"}} java/identifier -.-> lab-117435{{"`String Substring Search in Java`"}} java/arrays -.-> lab-117435{{"`String Substring Search in Java`"}} java/data_types -.-> lab-117435{{"`String Substring Search in Java`"}} java/if_else -.-> lab-117435{{"`String Substring Search in Java`"}} java/operators -.-> lab-117435{{"`String Substring Search in Java`"}} java/output -.-> lab-117435{{"`String Substring Search in Java`"}} java/strings -.-> lab-117435{{"`String Substring Search in Java`"}} java/variables -.-> lab-117435{{"`String Substring Search in Java`"}} java/string_methods -.-> lab-117435{{"`String Substring Search in Java`"}} java/system_methods -.-> lab-117435{{"`String Substring Search in Java`"}} end

Create a Java file

Let's create a Java file where we will write our Java code. Open the terminal and create a Java file using the following command:

touch ~/project/FindWordInString.java

Here, FindWordInString is the name of our Java file.

Find a Word in String using indexOf() method

In this step, we will use the indexOf() method to find the index of the specified substring in the given String. If the substring is present in the String, it returns its starting index, otherwise, it returns -1.

public class FindWordInString {
    public static void main(String[] args) {
        String str = "This sentence contains the word find me";
        System.out.println(str);

        String find = "find me";
        int index = str.indexOf(find);
        if (index >= 0) {
            System.out.println("Word found at index: " + index);
        } else {
            System.out.println("Word not found");
        }
    }
}

Run the code using the following command:

javac FindWordInString.java && java FindWordInString

You should see the output as:

This sentence contains the word find me
Word found at index: 31

Find a Word in String using contains() method

In this step, we will use the contains() method to check if the given String contains the specified substring or not. If it is present, it returns true, otherwise, it returns false.

public class FindWordInString {
    public static void main(String[] args) {
        String str = "This sentence contains the word find me";
        System.out.println(str);

        String find = "find me";
        boolean found = str.contains(find);
        if (found) {
            System.out.println("Word found");
        } else {
            System.out.println("Word not found");
        }
    }
}

Run the code using the following command:

javac FindWordInString.java && java FindWordInString

You should see the output as:

This sentence contains the word find me
Word found

Summary

In this lab, we learned how to find a word or substring in a Java String using indexOf() and contains() methods. We created a Java file and wrote the Java code to find the substring in the given String. We also learned how to run the Java code using the command line in Ubuntu.

Other Java Tutorials you may like