While working with Java, comparing two strings is one of the most common tasks. The startsWith() method is useful when checking if a string begins with a specific prefix. Since it is case-sensitive, it treats uppercase and lowercase letters differently. This method is commonly used in filtering, searching, and validating text-based inputs such as file names, URLs, and user inputs.
In this blog, we will learn how to compare two strings using the startsWith() Method in Java.
Table of Contents:
What is startsWith() method in Java?
The startsWith() method in Java is a built-in method of the String class that checks whether a string starts with a specified prefix. It returns true if the string begins with the given substring, otherwise, it returns false. This method is case-sensitive.
Syntax of startsWith() in Java
boolean result = string.startsWith(prefix);
Parameters of startsWith() in Java
The parameters of startsWith() in Java:
- string: The string to check.
- prefix: The substring that you want to check at the beginning of the string.
- result: The Boolean value (true if the condition is met, otherwise false)
Return value of startsWith() in Java
It returns a boolean value(either true or false).
- true: If the given string starts with the specified prefix
- false: If the given string does not start with the specified prefix.
Exceptions of startsWith() in Java
The startsWith() method in Java belongs to the String class and has a simple behavior when handling exceptions. It can throw NullPointerException, which may occur if the prefix (argument) is null.
Example:
Output:
Explanation: In the above code, we are checking if the string “Hello World” starts with null or not, which is not allowed, which causes a NullPointerException at runtime.
Example of startsWith() in Java
Here are the following examples of startsWith() in Java to compare two strings:
Example 1: Case Sensitivity Example using startsWith() Method in Java
We can check whether a string starts with a specific prefix using the startsWith() method. This method is case-sensitive, meaning it treats uppercase and lowercase letters differently.
Example:
Output:
Explanation: In the above code, the program checks if “Hello World” starts with “Hello” or “hello” using startsWith(), for which uppercase and lowercase letters are different. It then prints true for “Hello” and false for “hello”.
Example 2: Checking with a Different Prefix Using the startsWith() Method in Java
We can use the startsWith() method to check if a string begins with a different prefix. If the specified prefix does not match, the method returns false.
Example:
Output:
Explanation: In the above code,
- “Java Programming”.startsWith(“Java”) returns true because the string starts with “Java”.
- “Java Programming”.startsWith(“Prog”) returns false because “Prog” is not at the beginning of the string.
Example 3: Using startsWith() with Index in Java
The startsWith() method can also check if a string starts with a specific prefix from a given index position.
Example:
Output:
Explanation: In the above code,
- “Java Programming”.startsWith(“Programming”, 5) returns true because “Programming” starts at index 5 in the string.
- “Java Programming”.startsWith(“Java”, 0) returns true because “Java” starts at index 0.
Example 4: Using startsWith() for URL Validation
We can use startsWith() to check if a URL starts with “https://” or not.
Example:
Output:
Explanation: The above method checks if a URL starts with “https://” or not:
- url1.startsWith(“https://”) returns true because the url1 begins with “https://”.
- url2.startsWith(“https://”) returns false because the url2 does not start with “https://”, such as when it starts with “http://” or any other prefix.
Conclusion
So far in this blog, we have explored the startsWith() method in Java and its case-sensitive behavior. We learned how it checks if a string begins with a specific prefix, we also explored some examples of using different prefixes and how the method performs when checking from a specific index.
If you want to be an expert in Java Programming language, you may refer to our Java Course.
Some Other Methods to Compare Strings in Java