我有一个与C#中字符串操作有关的问题。
说我有一个字符串:
"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>
我想成功:
"Today I ate a chocolate at the supermarket. It was a brand of Contoso.
我可以删除</a>
部分,但不确定如何删除<a href
和>
我将如何做到这一点?
提前致谢!
答案 0 :(得分:1)
在这里找到了一个很好的答案:Need regular expression to remove <a href="xx">Name</a> tags from a string
也一样!
随意发布任何更好,更有效的方法。
答案 1 :(得分:0)
Regex
可能是最好的选择,但是如果你不想使用Regex
,那么以你想要的方式解析字符串会非常繁琐。
一个想法可能是按</a>
分割字符串,然后抓住<a
和>
和/ {p>的ethier方面的所有字符
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());
所以我会坚持Regex
如果它为你工作比使用字符串选项容易得多