Reference · Strings

ends_with

signature
ends_with(str, str) -> bool

What it does

Whether the text ends with a suffix. The comparison is exact: case matters and the suffix is plain text, not a pattern. For a file extension in any case, lower the text first.

Examples

print(ends_with("hello", "lo"), ends_with("hello", "LO"))print(ends_with("photo.png", ".png"), ends_with("photo.PNG", ".png"))print(ends_with(lower("photo.PNG"), ".png"))
output
true falsetrue falsetrue

Edge cases

print(ends_with("hello", ""), ends_with("", ""))print(ends_with("hello", "hello"), ends_with("hello", "xhello"))print(ends_with("café", "fé"))
output
true truetrue falsetrue

Errors

An argument that is not a string, on either side, is a runtime error, not an error value: null and numbers are refused. Give a null field ?? "" first.

this one fails
print(ends_with(null, "a"))
output
line 1: `ends_with` expects a str as argument 1, got null

Chaining

In a pipeline the text comes first and the suffix is the argument:

print("x.txt" | ends_with(".txt"))print(["img.png", "a.jpg", "b.PNG"] | filter((f) => lower(f) | ends_with(".png")))print(`photos/${uuid()}.jpg` | ends_with(".jpg"))
output
true["img.png", "b.PNG"]true

See also

starts_with, contains, lower, slice, re.test

Thread
7Useful? Like it, or add to its thread below.

Thread 0 posts

On ends_with(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo