How to Check if a String Starts with a Specific Prefix in Java?
In Java, to determine if a string starts with a specific prefix, we use the startsWith()
method from the String
class. This method is useful in various scenarios, including command parsing, input validation, and data filtering. In this article, we will check if a String Starts with a Specific Prefix in Java.
Prerequisites:
- Basic understanding of Java programming language syntax.
- Knowledge of String Manipulation in Java.
We should be familiar with how strings function in Java, specifically with the String
class and its methods, to effectively use startsWith()
.
String Starts with a Specific Prefix in Java
The startsWith()
method checks if a given string starts with a specified prefix. It returns true
if the string begins with the prefix; otherwise, it returns false
.
Syntax:
public boolean startsWith(String prefix)
public boolean startsWith(String prefix, int offset)
startsWith(String prefix)
: Checks whether the string starts with the specified prefix.startsWith(String prefix, int offset)
: Checks if the string starts with the specified prefix starting at the given index.
Flow of Execution:
- The
startsWith()
method compares the prefix to the beginning of the string. - The method returns
true
if the prefix matches the start of the string. - It returns
false
if the prefix does not match or if the prefix is longer than the string.
Java Program to Check if a String Starts with a Specific Prefix in Java
Example 1: Basic Usage of startsWith()
import java.io.*;
class GFG {
public static void main(String[] args) {
String text = "Hello, World!"; // String to be checked
String prefix = "Hello"; // Prefix to check
// Check if 'text' starts with 'prefix'
boolean result = text.startsWith(prefix);
// Print the result
System.out.println(result);
}
}
Output
true
Explanation of the above Program:
- This Java program checks if the string
text
starts with the specifiedprefix
using thestartsWith()
method. - It initializes
text
with "Hello, World!" andprefix
with "Hello". - The result is
true
because "Hello, World!" starts with "Hello", and this result is printed.
Example 2: Using startsWith() with an Offset
import java.io.*;
class GFG {
public static void main(String[] args) {
String text = "Hello, World!"; // String to be checked
String prefix = "World"; // Prefix to check
int offset = 7; // Starting index for the check
// Check if 'text' starts with 'prefix' from index 7
boolean result = text.startsWith(prefix, offset);
// Print the result
System.out.println(result);
}
}
Output
true
Explanation of the above Program:
Here, we use startsWith()
with an offset of 7
to check if the substring starting from index 7
of text
matches the prefix "World"
. Since it does, the result is true
.
Example 3: Case Sensitivity
The startsWith() method is case-sensitive, meaning "Hello" and "hello" are considered different prefix.
import java.io.*;
public class GFG {
public static void main(String[] args) {
String text = "Hello, World!"; // String to be checked
String prefix = "hello"; // Prefix to check (note the lowercase 'h')
// Check if 'text' starts with 'prefix'
boolean result = text.startsWith(prefix);
// Print the result
System.out.println(result);
}
}
Output
false
Explanation of the above Program:
The startsWith()
method is case-sensitive. In this example, "Hello"
and "hello"
are considered different, so the method returns false
.
Edge Cases
- Empty String Prefix:
text.startsWith("")
will always returntrue
because any string starts with an empty prefix. - Null Prefix: Calling
text.startsWith(null)
will throw aNullPointerException
, as the method does not handle null prefixes.
Conclusion
The startsWith()
method in Java provides a straightforward way to check if a string begins with a specific prefix. Understanding how to use this method effectively can enhance our ability to manipulate and validate strings in Java applications.