1

I have a question relating to the manipulation of strings in C#.

Say I have a string:

"Today I ate a <a href="link here "> chocolate </a> at the <a href=\"another link here"> supermarket</a>. It was a brand of <a href=\"3rd link">Contoso</a>

I would like to make it:

"Today I ate a  chocolate at the supermarket. It was a brand of Contoso.

I can remove the </a> part of it, but am unsure of how to remove everything and anything between the <a href and the >

How would I go about doing this?

Thanks in advance!

Ahmad Mageed
  • 94,561
  • 19
  • 163
  • 174
ForeverLearning
  • 1,027
  • 1
  • 12
  • 30
  • 1
    Look [here](http://stackoverflow.com/questions/787932/using-c-sharp-regular-expressions-to-remove-html-tags) – tttony Mar 03 '13 at 02:09

2 Answers2

1

Found a good answer here: Need regular expression to remove <a href="xx">Name</a> tags from a string

Works too!

Feel free to post any better, more efficent methods.

Community
  • 1
  • 1
ForeverLearning
  • 1,027
  • 1
  • 12
  • 30
0

Regex is probably the best option,But if you don't want to use Regex it will be quite fiddly to parse the string the way you want.

One idea could be to Split the string by </a> then grab all the chars on ethier side of <a and >

 var result = new string (input.Split(new string[] { "</a>" }, StringSplitOptions.RemoveEmptyEntries)
    .SelectMany(s => s.Where((c, i) => i < s.IndexOf("<a") || i > s.IndexOf(">"))).ToArray());

So I would stick with the Regex if its working for you as its a lot easier than using string options

sa_ddam213
  • 42,848
  • 7
  • 101
  • 110