STRING INSTANCE METHODS
Java String endsWith() method
endsWith() checks whether text ends with a specified suffix. It returns truewhen the ending matches and falsewhen the text has no such ending.
Checking a string's ending
String failas = "uzduotis.java";
System.out.println(failas.endsWith(".java"));
System.out.println(failas.endsWith(".txt"));true
false
- The variable
failasstores the text uzduotis.java. endsWith(".java")checks whether the text ends with the suffix .java.- Since the filename ending matches, the first result is
true. - .txt is not this string's ending, so the second result is
false.
Why is endsWith() an instance method?
endsWith() is called on a specific string because it checks that object's ending:
boolean tinka = failas.endsWith(".java");
We therefore do not write String.endsWith(...). The left-hand failas object is the text whose final characters are compared with the supplied suffix.
Method syntax
public boolean endsWith(String suffix)
| Part | Meaning |
|---|---|
| Calling object | failas in the line failas.endsWith(".java") |
| Parameter | suffix – the suffix to match against the string's ending |
| Parameter type | String |
| Return type | boolean |
true | The string ends with the supplied suffix |
false | The string does not end with the supplied suffix |
| Error | NullPointerExceptionwhen the argument is null |
Using endsWith() in a condition
String failas = "ataskaita.pdf";
if (failas.endsWith(".pdf")) {
System.out.println("Tai PDF dokumentas");
} else {
System.out.println("Kitas failo tipas");
}Tai PDF dokumentas
Because endsWith() returns boolean, it is convenient to use directly in an if condition. This is common for filenames, URLs, commands and user input.
Uppercase and lowercase letters differ
String failas = "Nuotrauka.JPG";
System.out.println(failas.endsWith(".jpg"));
System.out.println(failas.endsWith(".JPG"));false
true
endsWith() is case-sensitive. Nuotrauka.JPG ends with .JPGbut not with .jpgbecause the letters differ.
Checking without considering case
String failas = "Nuotrauka.JPG";
String galune = ".jpg";
boolean tinka = failas.toLowerCase().endsWith(galune.toLowerCase());
System.out.println(tinka);true
endsWith() has no parameter for ignoring case. For a simple file-extension check, you can convert both strings to lowercase using toLowerCase().
A suffix can be a longer substring
String zinute = "Sveiki atvykę į JavaQuest";
System.out.println(zinute.endsWith("JavaQuest"));
System.out.println(zinute.endsWith("į JavaQuest"));
System.out.println(zinute.endsWith("Sveiki"));true
true
false
A suffix need not be one character or a file extension. endsWith() checks whether the entire supplied sequence exactly matches the final part of the string.
An empty suffix always matches
String tekstas = "Java";
System.out.println(tekstas.endsWith(""));
System.out.println("".endsWith(""));true
true
Every string ends with the empty string "". Even an empty string is considered to end with an empty suffix, so both results are true.
Trailing spaces matter too
String komanda = "run ";
System.out.println(komanda.endsWith("run"));
System.out.println(komanda.endsWith(" "));
System.out.println(komanda.trim().endsWith("run"));false
true
true
endsWith() compares every character, including spaces. A string with a trailing space no longer ends with run. If trailing spaces are irrelevant, first use trim().
The connection to startsWith()
String adresas = publicLessonUrl('/java/string');
System.out.println(adresas.startsWith("https://"));
System.out.println(adresas.endsWith("/string"));true
true
startsWith() checks the start of a string, while endsWith() checks its ending. In the URL example, the first checks the protocol and the second checks the final path segment.
The argument cannot be null
String tekstas = "Java";
String galune = null;
System.out.println(tekstas.endsWith(galune));NullPointerException
If endsWith() receives null, Java has no suffix to compare with the string's ending. It throws NullPointerException.
Checking safely before endsWith()
String tekstas = "Java";
String galune = null;
if (galune != null && tekstas.endsWith(galune)) {
System.out.println("Pabaiga sutampa");
} else {
System.out.println("Galūnė nepateikta arba nesutampa");
}Galūnė nepateikta arba nesutampa
If the suffix may be null, check it first. The condition using && is safe because Java does not call endsWith()when galune != null is false.
Common mistakes
- Expecting
endsWith()to ignore uppercase and lowercase differences. - Forgetting that a trailing space is a real character.
- Passing a
nullargument to the method. - Confusing it with
contains():endsWith()checks only the end of the string, not any position. - Assuming that an empty suffix,
""should returnfalse.
When should you use endsWith()?
endsWith() Use it to check the last characters of text: a file extension, URL ending, command suffix or message format. To search anywhere in the string, use contains(). To check its start, use startsWith().