开发者

How to remove html tags from string in c#

开发者 https://www.devze.com 2023-01-22 16:05 出处:网络
I have something like this <h1> I am Text </h1> in asp.net MVC If I want to write it by <%: mystring%> that he show the tags who fille开发者_如何学Cd by someone.

I have something like this

<h1> I am Text </h1>

in asp.net MVC

If I want to write it by <%: mystring%> that he show the tags who fille开发者_如何学Cd by someone.

How I can decode it then show " I am Text "?

I just want to show the source from CKEDITOR to page without HTML tags. If I use regex then the all tags hide even user use to fill the information.


Use library such as HTML Agility Pack. You may use Regular Expressions but I wouldn't recommend it.

EDIT: Here's the sample code that does html to text conversion - http://htmlagilitypack.codeplex.com/SourceControl/changeset/view/66017#1336937


String result = Regex.Replace(htmlDocument, @"<[^>]*>", String.Empty);

More:

  • C# Remove HTML Tags
  • Matching HTML With Regular Expressions Redux
  • Html Agility Pack


Yes, I will be burned in hell for using regexes to work with html

var result = Regex.Replace(inputString, '<[^>]*>', string.Empty);

but this solution looks much simple than building FSM.


You can use Regex to remove those html tags. I hope you are aware of Regex class in C#.

Use this regex : <[^>]*?>

Replase all the matches with blank string ""

0

精彩评论

暂无评论...
验证码 换一张
取 消