C# 屏蔽关键字的实现方法
时间:2023-10-10 15:15:50
新建一个txt的文本(代码中读取这个文本文档路径就行,命名随意)
里面的内容一行代表一个,因为我是按行来遍历循环读取要屏蔽的关键字.然后用一个*号来屏蔽一个关键字,
例如: 在论坛中输出"草泥马",涉及到一些比较敏感的话题、名字,在一些推广比较火爆的网站里,都是不允许的,所以这里会只显示"***"。
这里代码下面我给出来了,注释都比较详细..不懂的可以留言问我.希望博友每天能进步一点点..
/// <summary>
/// 屏蔽非法字符串(如果有出现非法字符,那么用"***"来替换)
/// </summary>
/// <param name="strText">要检测的字符串</param>
/// <returns>返还一个健康的字符</returns>
public static string CheckKeyword(string strText)
{
IList<string> list = new List<string>(); //实例化一个数据集
string strpath = System.Web.HttpContext.Current.Server.MapPath("function/keyword.txt"); //获取文本文档路径
int a =strpath.LastIndexOf("IFSns");
int b =strpath.IndexOf("function");
string m = strpath.Substring(a+5, b - a - 6);
string PathTxt = strpath.Replace(m, ""); //获取调用这个方法的相对路径
FileStream fs = new FileStream(PathTxt, FileMode.Open, FileAccess.Read); //打开txt文档,将数据存到文件流中
StreamReader reader = new StreamReader(fs, Encoding.Default); //文件读取
string strLine = reader.ReadLine();
while (strLine!=null&&strLine.Length != 0) //有数据
{
list.Add(strLine.Trim().Replace(" ","")); //如果读取到的数据有空格,则删除空格,并且存到string数据集中
strLine = reader.ReadLine(); //每读取一次,从该行下一行开始继续读取
}
fs.Close(); //关闭文件流
foreach (string str in list) //循环遍历文件流
{
if (strText.Contains(str))
{
int lg = str.Length;
string sg = "";
for (int i = 0; i < lg; i++)
{
sg+="*";
}
strText = strText.Replace(str, sg); //如果含有txt文档中的关键字,则替换为"***"
}
}
return strText;
}
标签:C#,屏蔽关键字
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#实现调用本机摄像头实例
2022-07-01 19:54:49
浅谈SpringMVC请求映射handler源码解读
2022-09-11 14:37:36
![](https://img.aspxhome.com/file/2023/4/132134_0s.png)
SpringBoot事件机制相关知识点汇总
2021-05-29 06:53:45
浅析Java Web错误/异常处理页面
2022-02-14 00:13:50
![](https://img.aspxhome.com/file/2023/4/121954_0s.jpg)
MyBatis-Plus工具使用之EntityWrapper解析
2021-10-09 06:13:48
基于Java代码操作Redis过程详解
2022-03-28 06:53:58
![](https://img.aspxhome.com/file/2023/9/113259_0s.jpg)
Spring5源码解析之Spring中的异步和计划任务
2022-09-06 02:14:04
Android 开发使用PopupWindow实现弹出警告框的复用类示例
2022-04-07 03:43:42
![](https://img.aspxhome.com/file/2023/5/138855_0s.png)
C# 透明窗体制作实现方法比较分析
2022-11-26 02:35:30
![](https://img.aspxhome.com/file/2023/9/77269_0s.gif)
Idea springboot springCloud热加载热调试两种常用方式
2021-06-11 22:20:06
![](https://img.aspxhome.com/file/2023/3/122953_0s.png)
Java针对ArrayList自定义排序的2种实现方法
2022-01-30 03:47:34
基于c# Task自己动手写个异步IO函数
2021-08-06 12:07:56
![](https://img.aspxhome.com/file/2023/4/108664_0s.png)
Java使用Jdbc连接Oracle执行简单查询操作示例
2021-07-25 03:34:25
![](https://img.aspxhome.com/file/2023/8/63408_0s.jpg)
C#集合遍历时删除和增加元素的方法
2021-12-11 18:53:24
java实现一个简单TCPSocket聊天室功能分享
2022-06-11 20:04:36
EasyExcel实现导入+各种数据校验功能
2022-05-12 13:05:26
![](https://img.aspxhome.com/file/2023/1/71921_0s.png)
C++中智能指针如何设计和使用
2023-03-02 01:43:53
![](https://img.aspxhome.com/file/2023/7/138747_0s.jpg)
Android使用Gradle依赖配置compile、implementation与api的区别介绍
2023-09-27 18:11:40
Android Location服务之LocationManager案例详解
2022-02-03 00:01:11
![](https://img.aspxhome.com/file/2023/6/139586_0s.png)
Entity Framework模型优先与实体对象查询
2022-11-18 07:19:36
![](https://img.aspxhome.com/file/2023/4/85704_0s.png)