Java String startsWith() Method

The String.startsWith() method in Java is used to check if a string starts with a specified prefix. 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. startsWith Method Syntax
  3. Examples
    • Checking for a Prefix
    • Checking for a Prefix from a Specific Index
    • Case Sensitivity
    • Handling Edge Cases
  4. Conclusion

Introduction

The String.startsWith() method is a member of the String class in Java. It allows you to determine if a string begins with a specified prefix. This method is particularly useful for validating input, filtering strings, and performing prefix-based searches.

startsWith Method Syntax

The startsWith method has two common variations:

  1. Checking if a string starts with a specified prefix:
public boolean startsWith(String prefix)
  1. Checking if a string starts with a specified prefix from a specific index:
public boolean startsWith(String prefix, int toffset)
  • prefix: The prefix to be checked.
  • toffset: The starting index to begin checking the prefix.

Examples

Checking for a Prefix

The startsWith method can be used to check if a string starts with a specified prefix.

Example

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

        boolean startsWithHello = message.startsWith("Hello");
        boolean startsWithWorld = message.startsWith("World");

        System.out.println("Starts with 'Hello': " + startsWithHello);
        System.out.println("Starts with 'World': " + startsWithWorld);
    }
}

Output:

Starts with 'Hello': true
Starts with 'World': false

Checking for a Prefix from a Specific Index

The startsWith method can be used to check if a string starts with a specified prefix from a specific index.

Example

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

        boolean startsWithWorldFrom7 = message.startsWith("World", 7);

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

Output:

Starts with 'World' from index 7: true

Case Sensitivity

The startsWith method is case-sensitive, meaning it will distinguish between uppercase and lowercase characters.

Example

public class StartsWithExample {
    public static void main(String[] args) {
        String message = "hello, World!";

        boolean startsWithHello = message.startsWith("Hello");

        System.out.println("Starts with 'Hello': " + startsWithHello);
    }
}

Output:

Starts with 'Hello': false

Handling Edge Cases

The startsWith method can handle edge cases such as checking prefixes in an empty string or comparing with an empty prefix.

Example

public class StartsWithExample {
    public static void main(String[] args) {
        String emptyString = "";
        String message = "Hello, World!";

        boolean startsWithEmpty = message.startsWith("");
        boolean emptyStartsWithHello = emptyString.startsWith("Hello");

        System.out.println("Non-empty string starts with empty prefix: " + startsWithEmpty);
        System.out.println("Empty string starts with 'Hello': " + emptyStartsWithHello);
    }
}

Output:

Non-empty string starts with empty prefix: true
Empty string starts with 'Hello': false

Conclusion

The String.startsWith() method in Java is a simple and effective way to check if a string starts with a specified prefix. By understanding how to use this method, you can efficiently validate, filter, and search strings in your Java applications. Whether you are checking for prefixes, handling case sensitivity, or dealing with edge cases, the startsWith method provides a reliable solution for these tasks.

Comments