using System.Collections.Generic;
var strings = new List<(string part, string full)> { ("abcdefgh", "cdefghijklmno"), ("abcdefgh", "efghijklmno"), ("aaabbb", "abbbfffxx"), ("not inside", "abcfgh") };
AddLongRandomStrings(strings, 10, 100000, 10000);
var sw = System.Diagnostics.Stopwatch.StartNew();
foreach (var x in strings)
string result = RemovePartFromStart(x.full, x.part, out string prefixFound);
Console.WriteLine($"Found <{prefixFound}>:" + result);
Console.WriteLine("Not found");
TimeSpan elapsed = sw.Elapsed;
Console.WriteLine(elapsed);
public static string RemovePartFromStart(string full, string part, out string prefixFound)
for (int i = 0; i < part.Length; i++)
string part2 = part.Substring(i);
if (full.StartsWith(part2))
return full.Substring(part2.Length);
private static void AddLongRandomStrings(List<(string part, string full)> strings, int count, int fullStringLength, int partStringLength)
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for (int i = 0; i < count; i++)
char[] randomFullCharArray = new char[fullStringLength];
for (int j = 0; j < randomFullCharArray.Length; j++)
randomFullCharArray[j] = chars[r.Next(chars.Length)];
char[] randomPartCharArray = new char[partStringLength];
for (int j = 0; j < randomPartCharArray.Length; j++)
randomPartCharArray[j] = chars[r.Next(chars.Length)];
strings.Add((new string(randomPartCharArray), new string(randomFullCharArray)));