You need to write an extension method for the string class ... something like:
public static class StringExtensions
{
public static string ReplaceIf(this string text, string oldValue, string newValue)
{
if (oldValue is null) throw new ArgumentException($"{nameof(oldValue)} is null");
if (oldValue == string.Empty) throw new ArgumentException($"{nameof(oldValue)} is empty");
if (text.Contains(newValue)) return text;
return text.Replace(oldValue, newValue);
}
}
Now you can do this:
public class Program
{
public static void Main()
{
const string SiteAssets = "siteassets";
const string SysSiteAssets = "syssiteassets";
string text = $"{SiteAssets}/12345";
text = text.ReplaceIf(SiteAssets, SysSiteAssets);
text = text.ReplaceIf(SiteAssets, SysSiteAssets);
text = text.ReplaceIf(SiteAssets, SysSiteAssets);
text = text.ReplaceIf(SiteAssets, SysSiteAssets);
text = text.ReplaceIf(SiteAssets, SysSiteAssets);
Console.WriteLine(text);
Console.ReadLine();
}
}
The output will be:
syssiteassets/12345
Enjoy!