Click here to Skip to main content
15,887,450 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello, my question is a bit weird. Suppose I have this piece of code:


C#
public class Program
{
	public static void Main()
    {
		const string SiteAssets = "siteassets";
		const string SysSiteAssets = "syssiteassets";

	    string text = $"{SiteAssets}/12345";

		text = text.Replace(SiteAssets, SysSiteAssets);
		text = text.Replace(SiteAssets, SysSiteAssets);

		Console.WriteLine(text);

		Console.ReadLine();
	}


}


I'm getting "syssyssiteassets/12345" in the console. How do I change the code to receive just "syssiteassets/12345", no matter how many times this line will be called?
C#
text = text.Replace(SiteAssets, SysSiteAssets);


What I have tried:

Seems easy enough, but I can't use REGEX, nor additional if statements to achieve this goal.
Posted
Updated 11-Jul-22 5:11am

With a Replace, you can't - it's a very basic string match-and-replace function that doesn't do anything "clever". All it does is look for an exact match in the input string with the search string and does a direct replacement of it with the new string.
Since your replacement string contains the search string, it will always be found, and the Replace function has no concept of "start of string".

If you can't use a regex, then the only way to do it is to manually check is the input string starts with the search string, and only replace it if it does. But then you might as well use string.IndexOf and string.Substring yourself instead of Replace!
 
Share this answer
 
You need to write an extension method for the string class ... something like:
C#
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");
        // other qualifying test go here...

        if (text.Contains(newValue)) return text;

        return text.Replace(oldValue, newValue);
    }
}

Now you can do this:
C#
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!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900