一个简单的去除html标签的方法

Posted on 2010-09-24 13:33  少康  阅读(442)  评论(0编辑  收藏  举报

代码
using System;
using System.Collections.Generic;
using System.Web;

/// <summary>
/// 去除HTML代码的方法
/// </summary>
public class noHtml
{
public noHtml()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public string deleteHtml(string strBody)
{
try
{
for (int i = 0; i < Int32.MaxValue; i++)
{
if (strBody.IndexOf('<') > -1)
strBody
= strBody.Remove(strBody.IndexOf('<'), strBody.IndexOf('>') - strBody.IndexOf('<') + 1);
else
break;
}
}
catch (Exception ex)
{

}
return strBody.Replace("&nbsp;", "").Trim();
}
public string getImgUri(string strBody)
{
string uri = "http://www.bjmwh.com";
try
{
if (strBody.IndexOf("<img") > -1)
strBody
= uri + strBody.Substring(strBody.IndexOf("/controls/upfiles/image/"), strBody.IndexOf("/>") - strBody.IndexOf("/controls/upfiles/image/") - 2);
}
catch (Exception ex)
{
strBody
= org.mad.web.onyx.StrManage.GetString(strBody, 50);
}
return strBody;
}
}

 

通过遍历尖括号来循环删除HTML标签及空格,对转义字符无效。

Copyright © 2024 少康
Powered by .NET 9.0 on Kubernetes