Performance improvement for ReplaceFirst function (#13018)

* Some benchmarks for replacefirst & span method

(cherry picked from commit e550a4ce3a)
This commit is contained in:
patrickdemooij9
2023-03-23 21:11:59 +01:00
committed by Sebastiaan Janssen
parent 888173ac5b
commit eee6207f19
4 changed files with 78 additions and 2 deletions

View File

@@ -1040,14 +1040,15 @@ public static class StringExtensions
throw new ArgumentNullException(nameof(text));
}
var pos = text.IndexOf(search, StringComparison.InvariantCulture);
ReadOnlySpan<char> spanText = text.AsSpan();
var pos = spanText.IndexOf(search, StringComparison.InvariantCulture);
if (pos < 0)
{
return text;
}
return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
return string.Concat(spanText[..pos], replace.AsSpan(), spanText[(pos + search.Length)..]);
}
/// <summary>