0

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.

Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
Karthik Malla
  • 5,570
  • 12
  • 46
  • 89

5 Answers5

1

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");

Community
  • 1
  • 1
TheEye
  • 9,280
  • 2
  • 42
  • 58
1

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.

TheEye
  • 9,280
  • 2
  • 42
  • 58
0

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();
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • Thanks for your answer but I have just given an example code. Where in my original code a single bit gotta replace with 1000's of bit's so, changing the order will results to no use. I need a code where it independently runs each line without depending either on next nor before line... – Karthik Malla Feb 23 '11 at 09:12
  • I guess this will be possible using array statements... Not sure but just a fair guess...! – Karthik Malla Feb 23 '11 at 09:13
0

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();
}
xanatos
  • 109,618
  • 12
  • 197
  • 280
  • You have to add the `using System.Text.RegularExpressions` – xanatos Feb 23 '11 at 09:50
  • error namespace tuple and IEnumerable cannot be found :( Hope this code works but just a few errors please assist me with further... Thanks for your answer... – Karthik Malla Feb 23 '11 at 10:29
  • @Karthik right key on the code that has the error, Resolve and select the first line that starts with "using". The first example requires .NET 3.5, the second .NET 4.0. – xanatos Feb 23 '11 at 10:35
0

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

Qyuubi
  • 186
  • 5