public string RemoveScript(string str)
{
str = str.ToLower();
int length=str.Length;
ArrayList arr1 = new ArrayList();
ArrayList arr2 = new ArrayList();
for (int i = 0; i < length - 6;i++ )
{
if (str[i].ToString() == "<")
{
if (str[i + 1].ToString() == "s" && str[i + 2].ToString() == "c" && str[i + 3].ToString() == "r" &&
str[i + 4].ToString() == "i" && str[i + 5].ToString() == "p" && str[i + 6].ToString() == "t")
{
arr1.Add(i);
for (int j = i + 8; j < length - 6; j++)
{
if (str[j].ToString() == "<")
{
if (str[j + 1].ToString() == "/" && str[j + 2].ToString() == "s" && str[j + 3].ToString() == "c" && str[j + 4].ToString() == "r" &&
str[j + 5].ToString() == "i" && str[j + 6].ToString() == "p" && str[j + 7].ToString() == "t")
{
arr2.Add(j + 9);
i = j + 9;
break;
}
}
}//找邻近的"</script"位置
}
}
}//逐个查看“<script”的开始位置
string NoscriptStr = "";
if (arr1.Count==arr2.Count)
{
int arrlen = arr1.Count;
int subleft = 0;
int subright = 0;
string substr = "";
for (int k = 0; k < arrlen; k++)
{
subright = Convert.ToInt32(arr1[k].ToString());
substr = str.Substring(subleft, subright - subleft);
NoscriptStr = NoscriptStr + substr;
subleft = Convert.ToInt32(arr2[k].ToString());
}
substr = str.Substring(subleft, length - subleft);
NoscriptStr = NoscriptStr + substr;
}
return NoscriptStr;
}
c# 去除字符串中的script脚本
最新推荐文章于 2021-06-29 04:36:02 发布