Java – Check if this String Ends with a Specific Substring
To check whether a Java String ends with a specific substring, use the String.endsWith() method. It returns true when the string has the specified suffix and false otherwise.
This tutorial shows the standard endsWith() approach, explains its case-sensitive behavior and edge cases, and covers useful alternatives for case-insensitive checks and multiple possible suffixes.
Java String.endsWith() syntax and return value
The syntax of endsWith() function is as follows.
String.endsWith(String str)
endsWith() function checks if this string ends with the string passed as argument to the function.
The argument is the suffix to test. The method returns a boolean value. It compares characters exactly, so uppercase and lowercase letters are treated as different characters.
Following are some of the quick examples of endsWith().
"tutorialkart".endsWith("kart"); //true
"tutorialkart".endsWith("tuto"); //false
"tutorialkart".endsWith("t"); //true
"tutorialkart".endsWith("ia"); //false
How Java endsWith() matches a substring
endsWith() checks only the ending portion of the string. The suffix must match the final characters in the same order and with the same case.
String fileName = "report.pdf";
System.out.println(fileName.endsWith(".pdf"));
System.out.println(fileName.endsWith("pdf"));
System.out.println(fileName.endsWith(".PDF"));
true
true
false
The first two checks succeed because the requested suffix is present at the end of the string. The third check fails because endsWith() is case-sensitive.
Example 1 – Check If String Ends with a Substring – String.endsWith()
In this example, we have taken two strings: str1 and str2. After that we shall check if str1 ends with str2 using String.endsWith() method.
CheckIfStringEndsWith.java
/**
* Java Example Program to Check if String Ends with a String
*/
public class CheckIfStringEndsWith {
public static void main(String[] args) {
String str1 = "www\.tutorialkart.com";
String str2 = "com";
boolean b = str1.endsWith(str2);
System.out.print(b);
}
}
Run the above program and you should get the following output in console.
Output
true
Example 2 – Check If String Ends with a Substring
In this example, we have written a custom function, to check if a string ends with the specified string.
CheckIfStringEndsWith2.java
/**
* Java Example Program to Check if String Ends with
*/
public class CheckIfStringEndsWith2 {
public static void main(String[] args) {
System.out.println(ifEndsWith("www\.tutorialkart.com", "com"));
System.out.println(ifEndsWith("www\.tutorialkart.com", "www"));
}
/**
* Checks if str1 ends with str2
* @param str1
* @param str2
* @return return true if str1 ends with str2, else return false
*/
public static boolean ifEndsWith(String str1, String str2) {
if(str1.length()>=str2.length()) {
if(str1.substring(str1.length()-str2.length(), str1.length()).equals(str2)) {
return true;
}
}
return false;
}
}
Run the program.
Output
true
false
The custom method works by comparing a substring taken from the end of str1 with str2. In normal Java code, prefer the built-in endsWith() method because it states the intent directly and avoids manual index calculations.
Java endsWith() is case-sensitive
A suffix must have the same letter case as the characters at the end of the source string.
String name = "TutorialKart";
System.out.println(name.endsWith("Kart"));
System.out.println(name.endsWith("kart"));
true
false
Check whether a Java String ends with a substring ignoring case
String does not provide an endsWithIgnoreCase() method. For simple case-insensitive checks, you can compare the ending region with regionMatches() and enable its ignore-case option.
String text = "Report.PDF";
String suffix = ".pdf";
boolean matches = text.length() >= suffix.length()
&& text.regionMatches(
true,
text.length() - suffix.length(),
suffix,
0,
suffix.length()
);
System.out.println(matches);
true
This avoids changing either original string. Another common approach is to normalize both strings to the same case first, but locale-sensitive text needs additional care when case conversion is involved.
Check whether a Java String ends with any of multiple suffixes
When more than one suffix is acceptable, test each allowed value with endsWith(). For a small fixed set, boolean OR conditions are straightforward.
String fileName = "notes.txt";
boolean isTextFile = fileName.endsWith(".txt")
|| fileName.endsWith(".text");
System.out.println(isTextFile);
true
For a reusable list of suffixes, a stream can test whether any suffix matches.
String fileName = "photo.jpeg";
String[] suffixes = {".jpg", ".jpeg", ".png"};
boolean matches = java.util.Arrays.stream(suffixes)
.anyMatch(fileName::endsWith);
System.out.println(matches);
true
Does Java endsWith() support regular expressions?
No. The argument to endsWith() is treated as a literal suffix, not as a regular expression. If the ending condition is a pattern rather than a fixed substring, use Java’s regular-expression APIs instead.
String value = "invoice-2026.pdf";
boolean matchesPattern = value.matches(".*-\\d{4}\\.pdf");
System.out.println(matchesPattern);
true
Use endsWith() for a known literal suffix. Use a regular expression only when the ending itself follows a variable pattern.
Empty suffix and null suffix behavior in Java endsWith()
Every string ends with the empty string, so text.endsWith("") returns true. Passing null as the suffix causes a NullPointerException.
String text = "Java";
System.out.println(text.endsWith(""));
System.out.println("".endsWith(""));
true
true
If the suffix may be null, validate it before calling endsWith().
Java endsWith() compared with startsWith()
endsWith() checks a suffix at the end of a string, while startsWith() checks a prefix at the beginning. Choose the method based on which side of the string must match.
String url = "https://www.tutorialkart.com";
System.out.println(url.startsWith("https://"));
System.out.println(url.endsWith(".com"));
true
true
When to use String.endsWith() for substring checks in Java
Use String.endsWith() when you need to test a fixed suffix such as a file extension, domain ending, identifier suffix, or other literal text at the end of a string. Remember that the comparison is case-sensitive, the empty string is a valid suffix, and a null suffix is not accepted.
In this Java Tutorial, we learned how to check if a String ends with another string.
TutorialKart.com