How can I compare part of a string?
In .NetCore, or .Net framework with System.Memory nuget package:
str1.Length >= 5 && str2.Length >= 5 && str1.AsSpan(0, 5).SequenceEqual(str2.AsSpan(0, 5))
This is extremely heavily optimized, and will be the best performing of all the options here.
If your strings are at least 5 characters long, then string.Compare
should work:
var match = string.Compare(str1, 0, str2, 0, 5) == 0;
bool startsWithFoo = "foobar".StartsWith( "foo" );
Try this:
if (str1.Lenght >= 5 &&
str2.StartsWith(str1.Substring(0, 5)))
{
// Do what you please
}