Introducing ReplaceFirst() as string extension instead of using the CDF one

This commit is contained in:
Elitsa Marinovska
2020-03-17 10:21:43 +01:00
parent 0df497fd95
commit de9d82777f
2 changed files with 15 additions and 1 deletions

View File

@@ -938,7 +938,22 @@ namespace Umbraco.Core
return text;
}
/// <summary>
/// Returns a new string in which only the first occurrence of a specified string is replaced by a specified replacement string.
/// </summary>
/// <param name="text">The string to filter.</param>
/// <param name="search">The string to replace.</param>
/// <param name="replace">The replacement string.</param>
/// <returns>The filtered string.</returns>
public static string ReplaceFirst(this string text, string search, string replace)
{
var pos = text.IndexOf(search);
if (pos < 0)
return text;
return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
}