代码
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(" ", "").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标签及空格,对转义字符无效。