被过滤的字符列表可以存在数据库里。文本文件等
此例是放在文本文件中为Filter.txt
内容为
卑鄙|干|抄|超|乳|摸|玩|吻|倒霉|抠|骂|鳖|独|彪|婊|姬|阴|屁|臀|搂
private void FilterText()
{
string txtPath=Request.PhysicalApplicationPath+@"modules\guestbookmanager\filter.txt";
nn=this.TextBox1.Text;
if(!File.Exists(txtPath))
{
Response.Write ("文件路径或者文件路径不存在错误信息");
}
else
{
StreamReader objReader = new StreamReader(txtPath,System.Text.Encoding.GetEncoding("gb2312"));
string sLine="";
ArrayList arrText = new ArrayList();
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
arrText.Add(sLine);
}
objReader.Close();
foreach (string sOutput in arrText)
{
string[] strArr=sOutput.Split('|');
for (int i = 0; i < strArr.Length; i++)
{
string temp = "";
for (int j = 0; j < strArr[i].Length; j++)
{temp += "*";}
nn=nn.Replace(strArr[i], temp);
}
Response.Write(nn);
}
}
{
string txtPath=Request.PhysicalApplicationPath+@"modules\guestbookmanager\filter.txt";
nn=this.TextBox1.Text;
if(!File.Exists(txtPath))
{
Response.Write ("文件路径或者文件路径不存在错误信息");
}
else
{
StreamReader objReader = new StreamReader(txtPath,System.Text.Encoding.GetEncoding("gb2312"));
string sLine="";
ArrayList arrText = new ArrayList();
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
arrText.Add(sLine);
}
objReader.Close();
foreach (string sOutput in arrText)
{
string[] strArr=sOutput.Split('|');
for (int i = 0; i < strArr.Length; i++)
{
string temp = "";
for (int j = 0; j < strArr[i].Length; j++)
{temp += "*";}
nn=nn.Replace(strArr[i], temp);
}
Response.Write(nn);
}
}
过滤xtBox1.Text如果包含有Filter.txt的过滤字符时就替换等长的*号。
需要引用System.IO;命名空间