当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net 安全的截取指定长度的html或者ubb字符串

ASP.NET
asp.net GridView控件中模板列CheckBox全选、反选、取消
asp.net GridView 删除时弹出确认对话框(包括内容提示)
asp.net DropDownList 三级联动下拉菜单实现代码
asp DataTable添加列和行的三种方法
Asp.net 页面调用javascript变量的值
asp.net 长文章通过设定的行数分页
asp.net 定时间点执行任务的简易解决办法
asp.net 页面延时五秒,跳转到另外的页面
asp.net 动态输出透明gif图片
asp.net DataList与Repeater用法区别
asp.net Javascript获取CheckBoxList的value
asp.net程序在调式和发布之间图片路径问题的解决方法
asp.net下生成英文字符数字验证码的代码
asp.net 页面版文本框智能提示JSCode (升级版)
ASP.NET URL伪静态重写实现方法
ASP.NET 2.0 中Forms安全认证
asp.net 动态添加多个用户控件
asp.net Repeater显示父子表数据,无闪烁
asp.net 无法获取的内部内容,因为该内容不是文本 的解决方法
asp.net GridView排序简单实现

ASP.NET 中的 asp.net 安全的截取指定长度的html或者ubb字符串


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 150 ::
收藏到网摘: n/a

在将html代码输出到页面时,有时候会需要截断字符串保留指定长度的字符串,由于html中有些标签必须成对出现,所以在截取html时需要特别注意,不能因为截断问题把页面搞乱掉。 在截取字符串时需要记录每一个标签是否关闭,如果截取到指定长度还有没有关闭的标签,那么我们需要将标签关闭,或者删除掉未关闭的标签。不考虑某些不需要关闭标签的情况,html开始和结束标签总是成对出现的,我们可以遍历输入的字符串,并在标签开始时放入堆栈中,遇到结束标签时从堆栈中弹出一个元素,这样遍历到指定长度,堆栈中留下的标签就是需要补全或者删除掉的标签。
下面是代码实现,如果大家有更好的方法请给出来:
复制代码 代码如下:

static char END_SLASH = '/';
/// <summary>
/// 安全的截断字符串
/// </summary>
/// <param name="input">输入串</param>
/// <param name="length">截断长度</param>
/// <param name="trimHalfTag">true:截断半截标签;false:补全半截标签</param>
/// <param name="tagStartChar">标签开始字符</param>
/// <param name="tagEndChar">标签结束字符</param>
/// <param name="mustCloseTags">需要关闭的标签数组</param>
/// <returns>length长度的字符串</returns>
public static string SafeTrim(string input, int length, bool trimHalfTag, char tagStartChar, char tagEndChar, string[] mustCloseTags)
{
if (length <= 0) throw new ArgumentException("length 必须是正数");
if (mustCloseTags == null) throw new ArgumentNullException("mustCloseTags");
int inputLen = input.Length;
if (string.IsNullOrEmpty(input) || inputLen <= length) return input;
string result = string.Empty;
//声明堆栈用来放标签
Stack<string> tags = new Stack<string>();
for (int i = 0; i < length; i++)
{
char c = input[i];
if (c == tagStartChar)
{
string tag = string.Empty;
int tagIndex = i + 1;
bool isTagEnd = false;
bool isTagNameEnd = false;
result += c;
bool hasMarkTagInStack = false;
while (tagIndex < inputLen)
{
char tagC = input[tagIndex];
result += tagC;
tagIndex++;
if (tag == string.Empty && tagC == END_SLASH)
{
isTagEnd = true;
continue;
}
if (!isTagNameEnd)
{
if (char.IsLetter(tagC) || char.IsNumber(tagC))
{
tag += tagC;
}
else
{
isTagNameEnd = true;
}
}
if (!string.IsNullOrEmpty(tag))
{
if (isTagNameEnd && !hasMarkTagInStack)
{
if (isTagEnd)
{
tags.Pop();
}
else
{
tags.Push(tag);
}
hasMarkTagInStack = true;
}
}
if (isTagNameEnd)
{
if (tagC == tagEndChar)
{
i = tagIndex - 1;
break;
}
}
}
}
else
{
result += c;
}
}
while (tags.Count > 0)
{
string tag = tags.Pop();
bool isMustCloseTag = false;
foreach (string mustCloseTag in mustCloseTags)
{
if (string.Compare(mustCloseTag, tag, true) == 0)
{
isMustCloseTag = true;
break;
}
}
if (isMustCloseTag)
{
if (trimHalfTag)
{
int lastTagIndex = result.LastIndexOf(tagStartChar.ToString() + tag, StringComparison.CurrentCultureIgnoreCase);
result = result.Substring(0, lastTagIndex);
}
else
{
result += (tagStartChar.ToString() + END_SLASH + tag + tagEndChar);
}
}
}
return result;
}

转载请保留链接 玉开的技术博客