Update AbpStringExtensions.ReplaceFirst, to spans

pull/15878/head
Merijn Geurts 3 years ago committed by GitHub
parent e7f8fba5e6
commit 3218f2eb2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -209,7 +209,22 @@ public static class AbpStringExtensions
return str;
}
return str.Substring(0, pos) + replace + str.Substring(pos + search.Length);
var searchLength = search.Length;
var replaceLength = replace.Length;
var newLength = str.Length - searchLength + replaceLength;
Span<char> buffer = stackalloc char[newLength];
// Copy the part of the original string before the search term
str.AsSpan(0, pos).CopyTo(buffer);
// Copy the replacement text
replace.AsSpan().CopyTo(buffer.Slice(pos));
// Copy the remainder of the original string
str.AsSpan(pos + searchLength).CopyTo(buffer.Slice(pos + replaceLength));
return buffer.ToString();
}
/// <summary>

Loading…
Cancel
Save