Java 25 (LTS) reference · String Methods
Java String startsWith() Method
Return true if the string begins with a prefix. endsWith() checks the end the same way.
Java 25 (LTS) Since Java 1.0
Syntax
string.startsWith(prefix)
Parameters
| Parameter | Description |
|---|---|
prefixString |
The text to check for. |
Return value
true or false.
Examples
public class Main {
public static void main(String[] args) {
String path = "/admin/users";
System.out.println(path.startsWith("/admin"));
System.out.println(path.endsWith(".json"));
}
}
Output
true false
Run this on your computer — see “Get Started” for setup.
public class Main {
public static void main(String[] args) {
String[] files = {"report.pdf", "photo.png", "notes.pdf"};
for (String f : files) {
if (f.endsWith(".pdf")) System.out.println(f);
}
}
}
Output
report.pdf notes.pdf
Run this on your computer — see “Get Started” for setup.
Version history
| Version | Change |
|---|---|
| Java Java 1.0 | Added |
Related
string.contains()· Return true if the string contains a sequence of characters. Case-sensitive.- Java Strings · tutorial