1

So what I am trying to do is as follows :

example of a string is A4PC

I am trying to replace for example any occurance of "A" with "[A4]" so I would get and similar any occurance of "4" with "[A4]"

"[A4][A4]PC"

I tried doing a normal Replace on the string but found out I got

"[A[A4]]PC"

string badWordAllVariants =
                restriction.Value.Replace("A", "[A4]").Replace("4", "[A4]")

since I have two A's in a row causing an issue.

So I was thinking it would be better rather than the replace on the string I need to do it on a character per character basis and then build up a string again.

Is there anyway in Linq or so to do something like this ?

3
  • 2
    looks like your replace is running more than once. the replace itself works fine. Commented Dec 5, 2013 at 19:52
  • 1
    I tested a normal replace with AAPC and I got [A4][A4]PC... Maybe you are doing something else that is causing that behavior? Commented Dec 5, 2013 at 19:52
  • sorry yes, something else is happening im adding to comments now ! Commented Dec 5, 2013 at 19:54

3 Answers 3

5

You don't need any LINQ here - String.Replace works just fine:

string input = "AAPC";
string result = input.Replace("A", "[A4]"); // "[A4][A4]PC"

UPDATE: For your updated requirements I suggest to use regular expression replace

string input = "A4PC";
var result = Regex.Replace(input, "A|4", "[A4]"); // "[A4][A4]PC"
Sign up to request clarification or add additional context in comments.

1 Comment

ah wow ok that looks nice, so I could have a unlimited amount of chars in my string I could replace to "[A4]" by just appending A|4|5 etc ?
0

This works well for me:

        string x = "AAPC";
        string replace = x.Replace("A", "[A4]");

EDIT:

Based on the updated question, the issue is the second replacement. In order to replace multiple strings you will want to do this sequentially:

        var original = "AAPC";

        // add arbitrary room to allow for more new characters
        StringBuilder resultString = new StringBuilder(original.Length + 10);
        foreach (char currentChar in original.ToCharArray())
        {
            if (currentChar == 'A') resultString.Append("[A4]");
            else if (currentChar == '4') resultString.Append("[A4]");
            else resultString.Append(currentChar);
        }

        string result = resultString.ToString();

You can run this routine with any replacements you want to make (in this case the letters 'A' and '4' and it should work. If you would want to replace strings the code would be similar in structure but you would need to "look ahead" and probably use a for loop. Hopefully this helps!

By the way - you want to use a string builder here and not strings because strings are static which means space gets allocated every time you loop. (Not good!)

Comments

0

I think this should do the trick

string str = "AA4PC";

string result = Regex.Replace(str, @"(?<Before>[^A4]?)(?<Value>A|4)(?<After>[^A4]?)", (m) =>
{
    string before = m.Groups["Before"].Value;
    string after = m.Groups["After"].Value;
    string value = m.Groups["Value"].Value;

    if (before != "[" || after != "]")
    {
        return "[A4]";
    }

    return m.ToString();
});

It is going to replace A and 4 that hasn't been replaced yet for [A4].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.