Match the end of a string
Check the letters at the end of the text. The result is true only when those letters match.
String.EndsWith
Simple example
Does abc end with c?
Start'abc' · 'c'
ResultTrue
using System;
Console.WriteLine("abc".EndsWith("c", StringComparison.Ordinal));
True
Advertisements
Everyday examples
A teacher wants PDF files. Does this name end in .pdf?
Start'notes.pdf' · '.pdf'
ResultTrue
using System;
Console.WriteLine("notes.pdf".EndsWith(".pdf", StringComparison.Ordinal));
True
A music player lists MP3 files. Does the recording have that ending?
Start'rehearsal.wav' · '.mp3'
ResultFalse
using System;
Console.WriteLine("rehearsal.wav".EndsWith(".mp3", StringComparison.Ordinal));
False
Fragile shelf labels end in -F. Does this one belong there?
Start'12-F' · '-F'
ResultTrue
using System;
Console.WriteLine("12-F".EndsWith("-F", StringComparison.Ordinal));
True
Run the example
dotnet new console --output Lesson
cp Program.cs Lesson/Program.cs
dotnet run --project Lesson