Question

I do need a working ASP.NET C# code to replace characters in a string.

My following code works fine but in case of input "a" it gives me an output as "678d", but in case of input "c" the output is correct as it is i.e. "8d"... Here it automatically replaces the rest values too. I can see the code is executing in a step by step process... This results me to get an overloaded output.

{
    StringBuilder builder = new StringBuilder();
    builder.Replace("a", "6b");
    builder.Replace("b", "7c");
    builder.Replace("c", "8d");
    return builder.ToString();
}

Now, I do need to replace "a" as just "6b" and it should not load the rest values.

Was it helpful?

Solution

To be more independent of the actual replacements you could do a two way replacement:

First replace the occurrences with a placeholder that will not appear in the string (like %%1%% to replace "a", %%2%% to replace "b" etc.).

Then in a second run, replace %%1%% with "8d", %%2%% with "7c" etc.

This will work in any case.

OTHER TIPS

Your code is falling though from one statement to the others - so first you replace the a with 6b, and then you replace the b of the 6b with 7c and so on.

So, in your specific case, you could turn around the order of your statements so that latter statements do not react on the earlier ones - like

builder.Replace("c", "8d");

builder.Replace("b", "7c");

builder.Replace("a", "6b");

You need to change the order of your replaces:

StringBuilder builder = new StringBuilder();
builder.Replace("c", "8d");
builder.Replace("b", "7c");
builder.Replace("a", "6b");
return builder.ToString();

You could just use spaces behind the letter. As in "a" with "a " and replace "a " with required text.

Try this:

Regex rx = new Regex("a|b|c");

string str = "abc";

MatchEvaluator matcher = match => {
    string value = match.Value;
    if (value == "a") {
        return "6b";
    } else if (value == "b") {
        return "7c";
    } else {
        return "8d";
    }
};

var str2 = rx.Replace(str, matcher);

or this if you don't want to use Regex:

static void Main(string[] args)
{
    var replacements = new[] {
        new Tuple<string, string>("a", "6b"), 
        new Tuple<string, string>("b", "6c"), 
        new Tuple<string, string>("c", "6a")
    };

    string str = "adbc";
    var str2 = MultipleReplace(str, replacements);
}

static string MultipleReplace(string str, IEnumerable<Tuple<string, string>> replacements) {
    StringBuilder str2 = new StringBuilder();

    for (int i = 0; i < str.Length; i++) {
        bool found = false;

        foreach (var rep in replacements) {
            if (str.Length - i >= rep.Item1.Length && str.Substring(i, rep.Item1.Length) == rep.Item1) {
                str2.Append(rep.Item2);
                i += rep.Item1.Length - 1;
                found = true;
                break;
            }
        }

        if (!found) {
            str2.Append(str[i]);
        }
    }

    return str2.ToString();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top