Java String startsWith() Method

The String.startsWith() method in Java is used to check if a string begins with a specified prefix.

Table of Contents

  1. Introduction
  2. startsWith Method Syntax
  3. Examples
    • Basic Usage
    • Case Sensitivity
    • Using startsWith with a Specified Offset
    • Handling Edge Cases
    • Real-World Use Case
  4. Conclusion

Introduction

The String.startsWith() method is a member of the String class in Java. It allows you to determine if a string starts with a specified prefix. This method is particularly useful for validation and filtering purposes.

startsWith() Method Syntax

The syntax for the startsWith method is as follows:

Checking if a String Starts with a Specified Prefix

public boolean startsWith(String prefix)

Checking if a String Starts with a Specified Prefix from a Specified Offset

public boolean startsWith(String prefix, int toffset)

Examples

Basic Usage

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 str = "Welcome to Java";
        boolean result = str.startsWith("Welcome");
        System.out.println("Starts with 'Welcome': " + result);
    }
}

Output:

Starts with 'Welcome': true

Case Sensitivity

The startsWith method is case-sensitive, meaning it will only return true if the exact prefix, including case, is found.

Example

public class CaseSensitiveExample {
    public static void main(String[] args) {
        String str = "Welcome to Java";
        boolean result1 = str.startsWith("welcome");
        boolean result2 = str.startsWith("Welcome");
        System.out.println("Starts with 'welcome': " + result1);
        System.out.println("Starts with 'Welcome': " + result2);
    }
}

Output:

Starts with 'welcome': false
Starts with 'Welcome': true

Using startsWith with a Specified Offset

The startsWith method can also be used to check if a string starts with a specified prefix, starting from a specified offset.

Example

public class StartsWithOffsetExample {
    public static void main(String[] args) {
        String str = "Welcome to Java Programming";
        boolean result = str.startsWith("Java", 11);
        System.out.println("Starts with 'Java' from index 11: " + result);
    }
}

Output:

Starts with 'Java' from index 11: true

Handling Edge Cases

Example: Checking an Empty Prefix

If the prefix is an empty string, the startsWith method will always return true.

public class EmptyPrefixExample {
    public static void main(String[] args) {
        String str = "Welcome to Java";
        boolean result = str.startsWith("");
        System.out.println("Starts with an empty string: " + result);
    }
}

Output:

Starts with an empty string: true

Example: Checking a Prefix Longer Than the String

If the prefix is longer than the string, the startsWith method will return false.

public class LongPrefixExample {
    public static void main(String[] args) {
        String str = "Java";
        boolean result = str.startsWith("Java Programming");
        System.out.println("Starts with 'Java Programming': " + result);
    }
}

Output:

Starts with 'Java Programming': false

Real-World Use Case

Example: Validating URLs

One common use case for startsWith is validating URLs to ensure they use a specific protocol.

public class ValidateUrlExample {
    public static void main(String[] args) {
        String url1 = "https://www.example.com";
        String url2 = "ftp://www.example.com";

        if (url1.startsWith("https://")) {
            System.out.println(url1 + " uses HTTPS.");
        } else {
            System.out.println(url1 + " does not use HTTPS.");
        }

        if (url2.startsWith("https://")) {
            System.out.println(url2 + " uses HTTPS.");
        } else {
            System.out.println(url2 + " does not use HTTPS.");
        }
    }
}

Output:

https://www.example.com uses HTTPS.
ftp://www.example.com does not use HTTPS.

In this example, the startsWith method is used to check if the URLs use the HTTPS protocol.

Conclusion

The String.startsWith() method in Java is a simple yet powerful tool for checking if a string begins with a specified prefix. It is case-sensitive and provides the flexibility to start the check from a specified offset. This method is particularly useful for validation and filtering strings in various applications. By understanding and utilizing the startsWith method, you can efficiently manage string prefix checks in your Java programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top