Java String.endsWith
Check the end of a filename when listing receipts saved as PDFs.
A simple example
String.endsWith
- Start with "abc" and "bc"
- Do this Compare the ending
- Result Yes
class Main {
public static void main(String[] args) throws Exception {
System.out.println("abc".endsWith("bc"));
}
}
true
Syntax
label.endsWith(".pdf")
JDK 21 method signatures (1)
public boolean endsWith(String suffix)Run these examples in Java
Save one example at a time as Main.java. Use JDK 21.
java Main.javaFind filenames with a receipt extension
A document picker displays receipt files alongside unrelated attachments. Filter by the expected ending so the clerk sees the right filenames before opening any of the files.
class Main {
public static void main(String[] args) throws Exception {
String label = "receipt.pdf";
System.out.println((label.endsWith(".pdf")) ? "yes" : "no");
}
}
yes
The final four characters match .pdf. A filename such as receipt.pdf.old would fail the check.
Select receipts for a PDF list
A folder contains active exports alongside files with similar names and additional suffixes. Inspect the final filename or extension carefully before placing an entry in the report.
class Main {
public static void main(String[] args) throws Exception {
String name = "May-receipt.pdf";
System.out.println(name.endsWith(".pdf"));
}
}
true
The final four characters match the extension.
Exclude a backup copy
An export folder contains an active file and a backup with a longer name. Check the true ending of each name so the selection does not accidentally include the archived copy.
class Main {
public static void main(String[] args) throws Exception {
String name = "May-receipt.pdf.bak";
System.out.println(name.endsWith(".pdf"));
}
}
false
The final suffix is .bak even though .pdf appears earlier.
What to watch for
A filename suffix describes a naming convention. It cannot verify the contents of a file.