Library contents

Groovy 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.
Lesson.groovy
String value = "abc"
String other = "bc"
println((value.endsWith(other)) ? "yes" : "no")
Output
yes

Syntax

Groovy syntax
label.endsWith(".pdf")
Run these examples in Groovy

Save one example at a time as Lesson.groovy. Use Groovy 4 on JDK 21.

groovy Lesson.groovy

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.

Resultyes
Lesson.groovy
def label = "receipt.pdf"
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

Find a class roster

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 value = "class.csv"
Resultyes
Lesson.groovy
String value = "class.csv"
String other = ".csv"
println((value.endsWith(other)) ? "yes" : "no")
Output
yes

The final characters match the requested extension.

Exclude a backup export

A registry keeps an earlier export by adding an extra extension to its filename. Test the filename's final suffix before treating the file as part of today's import.

From the programString value = "class.csv.old"
Resultno
Lesson.groovy
String value = "class.csv.old"
String other = ".csv"
println((value.endsWith(other)) ? "yes" : "no")
Output
no

The final extension is .old.

What to watch for

A filename suffix describes a naming convention. It cannot verify the contents of a file.

Documentation for String.endsWith