E.D.
E.D.

Reputation: 273

How to remove start and end html tag using C#?

If I have some html code, like:

<p>Some text</p><p>More text</p>

...and I want to remove the start and end tags of that string, so I end up with:

Some text</p><p>More text

What would the C# code look like? I want it to work with any tag type, if they have classes, etc. Just need to be able to remove the start and end tags.

Upvotes: 1

Views: 1879

Answers (2)

Nico Kr&#228;tschmer
Nico Kr&#228;tschmer

Reputation: 156

Use Regex

var item = "<p>Some text</p><p>More text</p>";
item = Regex.Replace(item,@"^<[^>^<.]*>","");
item = Regex.Replace(item,@"<[^>^<.]*>$","");
Console.WriteLine(item) //Will log Some text</p><p>More text

Regex Breakdown:

^: matches start of string

<: opening tag

>: closing tag

[^>^<.]*: exclude closing and opening tags inside tag and match any character except the excluded ones as often as possible

Do the same again just this time we match the end of the string with $at the end of the expression

Upvotes: 2

davyioner
davyioner

Reputation: 191

If the tags are always 3 chars u just remove the first and last 3 chars from the string. If you want to use this you can try string.Remove(0,2); for example. string.replace won't work because the string contains multiple

and you don't want them all removed.

Edit: I see you want it to work with any tag type, then I would try this: Find the first > and the last < in the string. And use those positions with 0 and string.length to string.remove the tags.

Upvotes: 0

Related Questions