str_ends_with

str_ends_with(string $haystack, string $needle): bool

Checks if a string ends with a given suffix.

C C23

C23
#include <stdio.h>
#include <string.h>

// Hand-written idiom: compare the equal-length tail segment (a hit is impossible if it is longer than the shorter string)
static int str_ends_with(const char *h, const char *n) {
    size_t lh = strlen(h), ln = strlen(n);
    if (ln > lh) return 0;
    return strcmp(h + lh - ln, n) == 0;
}

int main(void) {
    printf("%d %d %d\n", str_ends_with("photo.jpg", ".jpg"),
           str_ends_with("photo.jpg", ".png"), str_ends_with("photo", ""));
    return 0;
}
Output
1 0 1

C++ C++26

C++26
#include <iostream>
#include <string>

// Hand-written idiom: compare the equal-length tail segment
static bool str_ends_with(const std::string &h, const std::string &n) {
    return h.size() >= n.size() &&
           h.compare(h.size() - n.size(), n.size(), n) == 0;
}

int main() {
    std::cout << str_ends_with("photo.jpg", ".jpg") << " "
              << str_ends_with("photo.jpg", ".png") << " "
              << str_ends_with("photo", "") << "\n";
    return 0;
}
Output
1 0 1

C# v11.0.100

v11.0.100
// EndsWith tests the suffix; the empty string is always true
Console.WriteLine("photo.jpg".EndsWith(".jpg"));
Console.WriteLine("photo.jpg".EndsWith(".png"));
Console.WriteLine("photo".EndsWith(""));
Output
True
False
True

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"strings"
)

func main() {
	// strings.HasSuffix tests the suffix; the empty string is always true
	fmt.Println(strings.HasSuffix("photo.jpg", ".jpg"))
	fmt.Println(strings.HasSuffix("photo.jpg", ".png"))
	fmt.Println(strings.HasSuffix("photo", ""))
}
Output
true
false
true

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        // endsWith tests the suffix; the empty string is always true
        System.out.println("photo.jpg".endsWith(".jpg"));
        System.out.println("photo.jpg".endsWith(".png"));
        System.out.println("photo".endsWith(""));
    }
}
Output
true
false
true

JavaScript v26.13.1

v26.13.1
// endsWith tests the suffix (introduced in ES2015; the empty string is always true)
console.log("photo.jpg".endsWith(".jpg"));
console.log("photo.jpg".endsWith(".png"));
console.log("photo".endsWith(""));
Output
true
false
true

PHP v8.6.0

v8.6.0
// Available since PHP 8.0: tests the suffix, returning a bool directly
var_dump(str_ends_with("photo.jpg", ".jpg"));
var_dump(str_ends_with("photo.jpg", ".png"));
// The empty suffix is always true
var_dump(str_ends_with("photo", ""));
Output
bool(true)
bool(false)
bool(true)

Python v3.15.2

v3.15.2
# endswith tests the suffix (the empty string is always True)
print("photo.jpg".endswith(".jpg"))
print("photo.jpg".endswith(".png"))
print("photo".endswith(""))
Output
True
False
True

Rust v1.99.1

v1.99.1
fn main() {
    // ends_with tests the suffix; the empty string is always true
    println!("{}", "photo.jpg".ends_with(".jpg"));
    println!("{}", "photo.jpg".ends_with(".png"));
    println!("{}", "photo".ends_with(""));
}
Output
true
false
true

TypeScript v26.13.1

v26.13.1
// endsWith tests the suffix (introduced in ES2015; the empty string is always true)
console.log("photo.jpg".endsWith(".jpg"));
console.log("photo.jpg".endsWith(".png"));
console.log("photo".endsWith(""));
Output
true
false
true

Language differences

C: the idiom is a length guard followed by strcmp on the tail slice; even C23's standard library has no ends_with.

C++: the idiom is a size() guard plus compare(tail offset, length, $needle); ends_with only becomes standard in C++20.

C#: string.EndsWith.

Go: strings.HasSuffix.

Java: String.endsWith.

JavaScript: String.prototype.endsWith (ES2015; before that the idioms were a $-anchored regex or slicing the tail for comparison).

PHP: introduced in PHP 8.0, replacing the old substr($haystack, -strlen($needle)) === $needle (negative-offset tail slice; careful when the needle is empty because substr then returns the whole string).

Python: str.endswith (also accepts a tuple to test several suffixes at once, common for file-extension filtering).

Rust: str::ends_with.

TypeScript: same as JavaScript's endsWith.