Performance improvement for ReplaceFirst function (#13018)
* Some benchmarks for replacefirst & span method
This commit is contained in:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user