String Searching Methods in Java

Introduction

Java provides several methods for searching within strings. These methods allow you to check for the presence of a substring, find its position, and more. This tutorial will cover various string searching methods with examples.

Table of Contents

  1. contains()
  2. indexOf()
  3. lastIndexOf()
  4. startsWith()
  5. endsWith()
  6. matches()
  7. Complete Example Program
  8. Conclusion

1. contains()

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

Syntax:

public boolean contains(CharSequence s)

Example:

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

        System.out.println("Contains 'World': " + result);
    }
}

Output:

Contains 'World': true

2. indexOf()

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

Syntax:

public int indexOf(int ch)
public int indexOf(int ch, int fromIndex)
public int indexOf(String str)
public int indexOf(String str, int fromIndex)

Example:

public class IndexOfExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        int index1 = str.indexOf('o');
        int index2 = str.indexOf('o', 5);
        int index3 = str.indexOf("World");
        int index4 = str.indexOf("world"); // Case-sensitive

        System.out.println("Index of 'o': " + index1);
        System.out.println("Index of 'o' from index 5: " + index2);
        System.out.println("Index of 'World': " + index3);
        System.out.println("Index of 'world': " + index4);
    }
}

Output:

Index of 'o': 4
Index of 'o' from index 5: 8
Index of 'World': 7
Index of 'world': -1

3. lastIndexOf()

The lastIndexOf() method returns the index within the string of the last occurrence of the specified substring or character. It returns -1 if the substring or character is not found.

Syntax:

public int lastIndexOf(int ch)
public int lastIndexOf(int ch, int fromIndex)
public int lastIndexOf(String str)
public int lastIndexOf(String str, int fromIndex)

Example:

public class LastIndexOfExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        int index1 = str.lastIndexOf('o');
        int index2 = str.lastIndexOf('o', 5);
        int index3 = str.lastIndexOf("World");
        int index4 = str.lastIndexOf("world"); // Case-sensitive

        System.out.println("Last index of 'o': " + index1);
        System.out.println("Last index of 'o' from index 5: " + index2);
        System.out.println("Last index of 'World': " + index3);
        System.out.println("Last index of 'world': " + index4);
    }
}

Output:

Last index of 'o': 8
Last index of 'o' from index 5: 4
Last index of 'World': 7
Last index of 'world': -1

4. startsWith()

The startsWith() method checks if a string starts with the specified prefix.

Syntax:

public boolean startsWith(String prefix)
public boolean startsWith(String prefix, int toffset)

Example:

public class StartsWithExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        boolean result1 = str.startsWith("Hello");
        boolean result2 = str.startsWith("World", 7);

        System.out.println("Starts with 'Hello': " + result1);
        System.out.println("Starts with 'World' from index 7: " + result2);
    }
}

Output:

Starts with 'Hello': true
Starts with 'World' from index 7: true

5. endsWith()

The endsWith() method checks if a string ends with the specified suffix.

Syntax:

public boolean endsWith(String suffix)

Example:

public class EndsWithExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        boolean result = str.endsWith("World!");

        System.out.println("Ends with 'World!': " + result);
    }
}

Output:

Ends with 'World!': true

6. matches()

The matches() method tells whether this string matches the given regular expression.

Syntax:

public boolean matches(String regex)

Example:

public class MatchesExample {
    public static void main(String[] args) {
        String str1 = "Hello123";
        String str2 = "Hello";

        boolean result1 = str1.matches("[A-Za-z0-9]+");
        boolean result2 = str2.matches("[A-Za-z0-9]+");

        System.out.println("str1 matches regex: " + result1);
        System.out.println("str2 matches regex: " + result2);
    }
}

Output:

str1 matches regex: true
str2 matches regex: true

7. Complete Example Program

Here is a complete program that demonstrates the various string searching methods discussed above.

Example Code:

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

        // contains() method
        System.out.println("Contains 'World': " + str.contains("World")); // true

        // indexOf() method
        System.out.println("Index of 'o': " + str.indexOf('o')); // 4
        System.out.println("Index of 'o' from index 5: " + str.indexOf('o', 5)); // 8
        System.out.println("Index of 'World': " + str.indexOf("World")); // 7
        System.out.println("Index of 'world': " + str.indexOf("world")); // -1

        // lastIndexOf() method
        System.out.println("Last index of 'o': " + str.lastIndexOf('o')); // 8
        System.out.println("Last index of 'o' from index 5: " + str.lastIndexOf('o', 5)); // 4
        System.out.println("Last index of 'World': " + str.lastIndexOf("World")); // 7
        System.out.println("Last index of 'world': " + str.lastIndexOf("world")); // -1

        // startsWith() method
        System.out.println("Starts with 'Hello': " + str.startsWith("Hello")); // true
        System.out.println("Starts with 'World' from index 7: " + str.startsWith("World", 7)); // true

        // endsWith() method
        System.out.println("Ends with 'World!': " + str.endsWith("World!")); // true

        // matches() method
        String str1 = "Hello123";
        String str2 = "Hello";
        System.out.println("str1 matches regex [A-Za-z0-9]+: " + str1.matches("[A-Za-z0-9]+")); // true
        System.out.println("str2 matches regex [A-Za-z0-9]+: " + str2.matches("[A-Za-z0-9]+")); // true
    }
}

Output:

Contains 'World': true
Index of 'o': 4
Index of 'o' from index 5: 8
Index of 'World': 7
Index of 'world': -1
Last index of 'o': 8
Last index of 'o' from index 5: 4
Last index of 'World': 7
Last index of 'world': -1
Starts with 'Hello': true
Starts with 'World' from index 7: true
Ends with 'World!': true
str1 matches regex [A-Za-z0-9]+: true
str2 matches regex [A-Za-z0-9]+: true

8. Conclusion

Java provides several methods to search within strings, each suited for different needs. The contains() method checks for the presence of a substring, while indexOf() and lastIndexOf() methods find the position of a substring or character. The startsWith() and endsWith() methods check for specific prefixes and suffixes. The matches() method allows you to check if a string matches a given regular expression. Understanding these methods will help you manipulate and analyze strings more effectively in Java.

Comments