tags:

views:

113

answers:

7
+1  Q: 

a replace question

I have this string

aa**b**qqidjwljd**p**fjem

I need to replace b by p and p by b

aa**p**qqidjwljd**b**fjem

the way I do this look like this

myvar.replace("b","1").replace("p","b").replace("1","p")

this is kind of really ugly

is there a better way?

edit

why ugly? because I have to decide/find an arbitrary set of characters that will work for any possible case

A: 

I can't think of a better way at the moment, really your method isn't that ugly.

Assuming that there is never a 1 in the string, it should work just fine.

chills42
A: 

Nope. It's the same as the standard swapping variables problem. (In order to swap the values of A and B, you need to use C).

Agent_9191
Even with Regex?
jarrett
If you're looking to swap 2 characters around you still have to change it to an intermediary. You can't do a .Replace('B', 'P').Replace('P', 'B') since you'll wind up with all B's. So even with Regex it's still the same thing.
Agent_9191
Thanks. Wasn't sure if Regex did all operations in place or chained. I would have assumed it was in place if you were using captures.
jarrett
Or you can look at Nestor's answer to do it as one group...: http://stackoverflow.com/questions/1651729/a-replace-question/1651788#1651788
Agent_9191
+2  A: 

I think this should do it, i think it a lot more readable

      private static string Transpose(string s)
  {
     string output = "";
     foreach (char c in s)
     {

        switch (c)
        {
           case 'P':
              output += 'B';
              break;
           case 'B':
              output += 'P';
              break;
           default:
              output += c;
              break;
        }
     }
     return output;
  }

does not win any prizes for open closed principle though!

Kev Hunter
Decent approach, but I'd use a StringBuilder instead of just strings.
Agent_9191
Yeah I should have used a stringbuilder
Kev Hunter
+6  A: 
string sold = "aa**b**qqidjwljd**p**fjem";
string snew = Regex.Replace(sold, "(?<replace>b|p)", delegate(Match p)
{
    switch (p.Groups["replace"].Value)
    {
        case "b": return "p";
        case "p": return "b";
    };
    throw new ApplicationException("Should never happen!");
});
Nestor
Damn you Regex experts! :)
Philip Wallace
Hey don't damn me... give me an up vote instead! :-)
Nestor
+1 Oops, sorry - too busy deciphering your answer...
Philip Wallace
A: 

Yours is as concise as I can think on a Friday afternoon. Depending on what Replace does internally, this might be quicker:

    char [] myChars = "aa**b**qqidjwljd**p**fjem".ToCharArray();

    for (int x = 0; x < myChars.Length; x++)
    {
        char currentCharacter = myChars[x];
        if (currentCharacter == 'b')
        {
            myChars[x] = 'p';
        }
        else if (currentCharacter == 'p')
        {
            myChars[x] = 'b';
        }
    }

    string myString = new string(myChars);
Philip Wallace
A: 

Your way works fine. The only problem would be is if there's ever a '1' in the string, as it will cause your logic to be corrupted.

A better solution would be to use a temporary character that you're sure will never appear in the text, like a pipe ('|') or a tilde ('~') for example.

Ken White
+1  A: 

this seem to work too

var a = "aa**b**qqidjwljd**p**fjem";
a = new string((from n in a select (n == 'b' ? 'p' : (n == 'p' ? 'b' : n))).ToArray());
Fredou