Match the start of a string
Check the letters at the start of the text. The result is true only when those letters match.
String.StartsWith
Simple example
Does abc start with a?
Start'abc' · 'a'
ResultTrue
using System;
Console.WriteLine("abc".StartsWith("a", StringComparison.Ordinal));
True
Advertisements
Everyday examples
Child tickets start with C-. Is this a child ticket?
Start'C-18' · 'C-'
ResultTrue
using System;
Console.WriteLine("C-18".StartsWith("C-", StringComparison.Ordinal));
True
Express parcels start with EX. Should this parcel go in the express tray?
Start'BX42' · 'EX'
ResultFalse
using System;
Console.WriteLine("BX42".StartsWith("EX", StringComparison.Ordinal));
False
Is this shelf in the LAB section?
Start'LAB-3' · 'LAB'
ResultTrue
using System;
Console.WriteLine("LAB-3".StartsWith("LAB", StringComparison.Ordinal));
True
Run the example
dotnet new console --output Lesson
cp Program.cs Lesson/Program.cs
dotnet run --project Lesson