Library contents

R endsWith

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

A simple example

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.R
value <- "abc"
other <- "bc"
cat(if (endsWith(value, other)) "yes" else "no", "\n", sep = "")
Output
yes

Syntax

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

Save one example at a time as Lesson.R. Use R 4.x.

Rscript --vanilla Lesson.R

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 programlabel <- "receipt.pdf"
Resultyes
Lesson.R
label <- "receipt.pdf"
cat(if (endsWith(label, ".pdf")) "yes" else "no", "\n", sep = "")
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 programvalue <- "class.csv"
Resultyes
Lesson.R
value <- "class.csv"
other <- ".csv"
cat(if (endsWith(value, other)) "yes" else "no", "\n", sep = "")
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 programvalue <- "class.csv.old"
Resultno
Lesson.R
value <- "class.csv.old"
other <- ".csv"
cat(if (endsWith(value, other)) "yes" else "no", "\n", sep = "")
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 endsWith