Thursday, February 28, 2013

How find html tag and remove it from string in C#


How find html tag from string in C#

   Situation: i want to find and remove all the javascript from mystring. which can be like

<script  type="text/javascript" id="someid">

.......Some functions goes here


Now i want to remove all the script content from my string. The best way to remove script content is by using  Regularexpression

            Regex reg=new Regex("<script (.+?)</script>");
            reg.Replace(test, "");


 string test = "<script type='text/javascript'> Some functions goes here </script> A C# string is an array of characters declared using the string keyword. A string literal is declared using quotation marks, as shown in the following example:   ";
            test += " <br/> You can extract substrings, and concatenate strings, like this:";
            test += "<script> another text </script> <br /> String objects are immutable, meaning that they cannot be changed once they have been created. Methods that act on strings actually return new string objects. In the previous example, when the contents of s1 and s2 are concatenated to form a single string, the two strings containing \"orange\" and \"red\" are both unmodified. The += operator creates a new string that contains the combined contents. The result is that s1 now refers to a different string altogether. A string containing just \"orange\" still exists, but is no longer referenced when s1 is concatenated.";
            Regex reg=new Regex("<script (.+?)</script>");

            reg.Replace(test, "");


No comments:

Post a Comment