当前位置: 移动技术网 > IT编程>开发语言>c# > C#使用for循环移除HTML标记

C#使用for循环移除HTML标记

2019年07月18日  | 移动技术网IT编程  | 我要评论

移除一段文字中的html标记,以消除其中包含的样式和段落等,最常用的办法可能就是正则表达式了。但是请注意,正则表达式并不能处理所有的html文档,所以有时采用一个迭代的方式会更好,如for循环。

看下面的代码:

using system;
using system.text.regularexpressions;
/// <summary>
/// methods to remove html from strings.
/// </summary>
public static class htmlremoval
{
/// <summary>
/// remove html from string with regex.
/// </summary>
public static string striptagsregex(string source)
{
return regex.replace(source, "<.*?>", string.empty);
}
/// <summary>
/// compiled regular expression for performance.
/// </summary>
static regex _htmlregex = new regex("<.*?>", regexoptions.compiled);
/// <summary>
/// remove html from string with compiled regex.
/// </summary>
public static string striptagsregexcompiled(string source)
{
return _htmlregex.replace(source, string.empty);
}
/// <summary>
/// remove html tags from string using char array.
/// </summary>
public static string striptagschararray(string source)
{
char[] array = new char[source.length];
int arrayindex = 0;
bool inside = false;
for (int i = 0; i < source.length; i++)
{
char let = source[i];
if (let == '<')
{
inside = true;
continue;
}
if (let == '>')
{
inside = false;
continue;
}
if (!inside)
{
array[arrayindex] = let;
arrayindex++;
}
}
return new string(array, 0, arrayindex);
}
}

代码中提供了两种不同的方式来移除给定字符串中的html标记,一个是使用正则表达式,一个是使用字符数组在for循环中进行处理。来看一下测试的结果:

using system;
using system.text.regularexpressions;
class program
{
static void main()
{
const string html = "<p>there was a <b>.net</b> programmer " +
"and he stripped the <i>html</i> tags.</p>";
console.writeline(htmlremoval.striptagsregex(html));
console.writeline(htmlremoval.striptagsregexcompiled(html));
console.writeline(htmlremoval.striptagschararray(html));
}
}

  输出结果如下:

there was a .net programmer and he stripped the html tags.
there was a .net programmer and he stripped the html tags.
there was a .net programmer and he stripped the html tags.

  上述代码中分别调用了htmlremoval类中的三个不同的方法,均返回了相同的结果,即去除了给定字符串中的html标记。推荐使用第二种方法,即直接引用一个预先定义好的regexoptions.compiled的正则表达式对象,它比第一种方法速度更快。但是regexoptions.compiled有一些缺点,在某些情况下它的启动时间会增加数十倍。具体的内容可以查看下面这两篇文章:

regexoption.compiled
regex performance

  通常,正则表达式的执行效率并不是最高的,所以htmlremoval类中给定了另一种方法,使用字符数组来处理字符串。测试程序提供了1000个html文件,每个html文件中有大约8000个字符,所有的文件均通过file.readalltext方式进行读取,测试结果显示字符数组的方式执行速度是最快的。

performance test for html removal

htmlremoval.striptagsregex: 2404 ms
htmlremoval.striptagsregexcompiled: 1366 ms
htmlremoval.striptagschararray: 287 ms [最快]

file length test for html removal

file length before: 8085 chars
htmlremoval.striptagsregex: 4382 chars
htmlremoval.striptagsregexcompiled: 4382 chars
htmlremoval.striptagschararray: 4382 chars

  所以,使用字符数组来处理大批量的文件时可以节省时间。在字符数组方法中,仅仅只是将非html标记的字符添加到数组缓冲区,为了提高效率,它使用字符数组和一个新的字符串构造器来接收字符数组和范围,这个会比使用stringbuilder速度更快。

对于自关闭的html标记

  在xhtml中,某些标记并不具有独立的关闭标签,如<br/>,<img/>等。上述代码应该能够正确处理自关闭的html标记。下面是一些支持的html标记,注意,正则表达式方法可能无法正确处理无效的html标记。

supported tags

<img src="" />
<img src=""/>
<br />
<br/>
< div >
<!-- -->

html文档中的注释

  本文给出的代码对移除html文档注释中的html标记可能会失效。有些时候,注释中可能会包含一些无效的html标记,在处理时这些html标记不会被完全移除。但是,扫描这些不正确的html标记有时可能是必要的。

如何验证

  有许多种方法可以用来验证xhtml,我们可以采用和上面代码相同的方式来进行迭代。一个简单的方法是对'<'和'>'进行计数,从而确定它们是否匹配,或者采用正则表达式进行匹配。这里有一些资源介绍了这些方法:

html brackets: validation

validate xhtml

  有许多方法都可以用来去除给定字符串中的html标记,它们返回的结果也都是正确的。毫无疑问,采用字符数组进行迭代的效率最高。

以上所述是小编给大家介绍的c#使用for循环移除html标记 ,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网