-2

I want to replace ALL HTML special entities like > < to custom string.

Lets say i have following string:

string str = "<div>&gt;hello&lt;</div>";

and method: Method(string str, string replaceStr)

After calling Method(str, ":)") result should be

<div>:)hello:)</div>

The problem is there are too many of special characters and I'm wondering what is the be most efficient way to accomplish this?

EDIT:

String.Replace will not do my work and using Regex for parsing HTML is not really good approach.

By dislikes on this quetion there propably isn't any clean solution therefore I decided go for following algorithm:

  1. create txt file with valid HTML special characters (like &para;)
  2. parse file into array of string
  3. Thanks to HtmLAgilityPack parse HTML and get raw text and replace all entities.

I know that this is not really effective for big html string but it should do the work for now.

Community
  • 1
  • 1

1 Answers1

2

You can try:

        string str = "<div>&gt;hello&lt;</div>";
        string output = Regex.Replace(str, "&gt;|&lt;", ":)");

You can also use HtmlDecode

        string str = "<div>&gt;hello&lt;</div>";
        string output = WebUtility.HtmlDecode(str);
Yanga
  • 2,885
  • 1
  • 29
  • 32