Library contents

Java String.endsWith

Check the end of a filename when listing receipts saved as PDFs.

A simple example

String.endsWith

  1. Start with "abc" and "bc"
  2. Do this Compare the ending
  3. Result Yes
The final two characters are exactly b and c.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        System.out.println("abc".endsWith("bc"));
    }
}
Output
true

Syntax

Java 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.java

Find 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.

From the programString label = "receipt.pdf"
Resultyes
Main.java

class Main {
    public static void main(String[] args) throws Exception {
        String label = "receipt.pdf";
        System.out.println((label.endsWith(".pdf")) ? "yes" : "no");
    }
}
Output
yes

The final four characters match .pdf. A filename such as receipt.pdf.old would fail the check.

Advertisements

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.

From the programString name = "May-receipt.pdf"
Resulttrue
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        String name = "May-receipt.pdf";
        System.out.println(name.endsWith(".pdf"));
    }
}
Output
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.

From the programString name = "May-receipt.pdf.bak"
Resultfalse
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        String name = "May-receipt.pdf.bak";
        System.out.println(name.endsWith(".pdf"));
    }
}
Output
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.

Documentation for String.endsWith