Match the end of a string
Check the letters at the end of the text. The result is true only when those letters match.
std.mem.endsWith
Simple example
Does abc end with c?
Start'abc' · 'c'
Resulttrue
const std = @import("std");
pub fn main() !void {
std.debug.print("{any}\n", .{std.mem.endsWith(u8, "abc", "c")});
}
true
Advertisements
Everyday examples
A teacher wants PDF files. Does this name end in .pdf?
Start'notes.pdf' · '.pdf'
Resulttrue
const std = @import("std");
pub fn main() !void {
std.debug.print("{any}\n", .{std.mem.endsWith(u8, "notes.pdf", ".pdf")});
}
true
A music player lists MP3 files. Does the recording have that ending?
Start'rehearsal.wav' · '.mp3'
Resultfalse
const std = @import("std");
pub fn main() !void {
std.debug.print("{any}\n", .{std.mem.endsWith(u8, "rehearsal.wav", ".mp3")});
}
false
Fragile shelf labels end in -F. Does this one belong there?
Start'12-F' · '-F'
Resulttrue
const std = @import("std");
pub fn main() !void {
std.debug.print("{any}\n", .{std.mem.endsWith(u8, "12-F", "-F")});
}
true