asp.net 安全的截取指定长度的html或者ubb字符串
程序员文章站
2024-03-08 13:23:41
在截取字符串时需要记录每一个标签是否关闭,如果截取到指定长度还有没有关闭的标签,那么我们需要将标签关闭,或者删除掉未关闭的标签。不考虑某些不需要关闭标签的情况,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;
}
转载请保留链接
下面是代码实现,如果大家有更好的方法请给出来:
复制代码 代码如下:
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;
}
转载请保留链接