1

I wrote the code below to replace strings but it seemed like there is something wrong with the code. It couldn't replace multiple times. It only can replace the first occurrence. It suppose to replace all occurrences.

//Do not use System.Security.SecurityElement.Escape. I am writing this code for multiple .NET versions.

public string UnescapeXML(string s)
{
    if (string.IsNullOrEmpty(s)) return s;
    return s.Replace("&amp;", "&").Replace("&apos;", "'").Replace("&quot;", "\"").Replace("&gt;", ">").Replace("&lt;", "<");
}

protected void Page_Load(object sender, EventArgs e)
{

    string TestData="TestData&amp;amp;amp;";
    Response.Write("using function====>>>>" + UnescapeXML(TestData));
    Response.Write("\n\n<BR>Not using function====>>>>" + TestData.Replace("&amp;", ""));

}
3
  • What should be the result for your given TestData? "TestData&"? Then you have to do a loop, till s.Conatins("&amp;") == false. Commented Jun 12, 2011 at 19:15
  • Is it me or is the MS recommended approach overly verbose? support.microsoft.com/kb/316063 Commented Jun 12, 2011 at 19:20
  • @Peter, that's a nice observation. It did not occur to me that SMTPGuy could be thinking recursively. Looking back, I don't think he was, but passersby to this question might need that solution. I added some clarification to my answer so that it refers to both the linear and recursive versions of string replacement. Commented Jun 12, 2011 at 19:29

2 Answers 2

5

Your function is operating correctly, assuming you want a one-pass (per substring) solution.

Your testDate is missing & characters before amp;

Change to:

string TestData="TestData&amp;&amp;&amp;";

From:

string TestData="TestData&amp;amp;amp;";

What is a one-pass (per substring) solution?

  1. Each unique substring1 occurrence is changed.
  2. Then, each unique substring2 occurrence is changed.
  3. ...
  4. Then, each unique substringN occurrence is changed.

Recursive Alternative

A recursive solution would involve the following:

  1. After one-single substring replacement, you call the function again

  2. If no replacements occured, return the string

    public string ReplaceStuff(string MyStr) {   
      //Attempt to replace 1 substring
    
      if ( ReplacementOccurred ){
        return ReplaceStuff(ModifiedString);
      }else{
        return MyStr;
      } 
    }
    

What do you get with Recursion?

  • Well, given an input string: &amp;amp;, it would first be changed to &amp;, then the next level of recursion would change it to &.
  • I honestly don't think the author wants the recursive solution, but I could see how passersby might.
Sign up to request clarification or add additional context in comments.

Comments

1

Looks like the HttpUtility class will be the best choise in that case.

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.