某游戏研究之字符过滤类-WorldFilter
所谓字符过滤器,常常用在聊天的内容,比如一连串的骂人难听的话,我们要屏蔽掉,避免造成不好的东西!
当然我作为中华天朝一个有文明有素质的人,肯定偶尔会做这样的事情啦,特别是打LOL的时候,算了不讲了,都是泪啊。
我们来看看这款游戏字符过滤是怎么实现的,结合我上节讲到的单例类,因为字符过滤类当然是个单例类。
public class WordFilter : Singleton<WordFilter>
{
private string[] m_StringFilters;
private Dictionary<int, string> m_Replacers = new Dictionary<int, string>();
public void Init()//先从Init开始看
{
string fullPath = ResourceManager.GetFullPath("config/table/language_filter.txt", false);//这个是你要过滤的词汇,这个txt里面存的都是不好的string
this.m_StringFilters = File.ReadAllLines(fullPath);//从txt中读取string存到m_StringFilters中
for (int i = 0; i < this.m_StringFilters.Length; i++)
{
string text = this.m_StringFilters[i].ToLower();
this.m_StringFilters[i] = text;//都变成小写格式
int length = text.Length;//单个string的长度
string text2 = new string('*', length);//根据要过滤字符的长度构造等长的*字符串
if (text2.Equals(text))//如果等于过滤的字符,那个这个就为空,其实就是用户不小心修改要过滤字符等于*,其实没必要
{
this.m_StringFilters[i] = null;
}
else
{
if (!this.m_Replacers.ContainsKey(length))//m_Replacers是个字典,key:length=>value:*,也就是多少长度的*字符串
{
this.m_Replacers.Add(length, text2);//如果不存在这个长度就加入
}
}
}
}
public bool FilterString(ref string originalString)//这个是过滤的核心代码,之前是初始化,主要初始过滤字符数组和过滤*字典
{
string a = originalString;//你聊天的一句话(注意是一整句)
string text = originalString.ToLower();//搞成小写
string[] stringFilters = this.m_StringFilters;
for (int i = 0; i < stringFilters.Length; i++)
{
string text2 = stringFilters[i];
if (!string.IsNullOrEmpty(text2))//不为空
{
int num = 0;
while (true)
{
num = text.IndexOf(text2, num);//从index=0开始,寻找text(也就是聊天的话)中第一个等于text2(也就是不好的string)的index
if (-1 == num)//也就是没找到就跳出循环
{
break;
}
string str = originalString.Substring(0, num);//截断聊天语句不好词的前面字符串
string str2 = originalString.Substring(num + text2.Length);//截断聊天语句不好词后面的字符串,直到结束
originalString = str + this.m_Replacers[text2.Length] + str2;//吧不好的词替换成*字符
str = text.Substring(0, num);//为什么要改text呢,主要是为了跳出循环,当text不再含有不好词,num就为-1,然后break
str2 = text.Substring(num + text2.Length);
text = str + this.m_Replacers[text2.Length] + str2;
}
}
}
return a == originalString;//判断originalString是否改了,如果改了就说明过滤了,返回false,如果没改就返回true
}
}
所以,我们聊天的string都需要经过过滤字符类的方法FilterString处理。
因为FilterString的参数是ref 引用类型,所以只要有不好的词就会改变原来的聊天语句。
某游戏研究之字符过滤类-WorldFilter的更多相关文章
- 华为-on练习--重复的字符过滤
称号: 请写一个字符串过滤程序,如果使用多个相同的字符出现在字符串中,字符首次出现在非过滤,. 比方字符串"abacacde"过滤结果为"abcde". 演示样 ...
- java web过滤器实际应用(解决中文乱码 html标签转义功能 敏感字符过滤功能)
转载地址:http://www.cnblogs.com/xdp-gacl/p/3952405.html 在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可 ...
- php安全过滤类
/*ansic码-Url码表: http://www.w3school.com.cn/tags/html_ref_urlencode.html ---------------------------- ...
- swift 字符转为类,代码创建控件
在使用类之前要先获得 命名空间 通过json来获取 字符型的类名 然后创建类对象,这时候就要用到字符转类 // 从info字典中获取到 命名空间 转为字符型 let NS = NSBundle.mai ...
- java 非法字符过滤 , 半角/全角替换
java 非法字符过滤 , 半角/全角替换 package mjorcen.netty.test1; import java.io.UnsupportedEncodingException; publ ...
- javaio学习笔记-字符流类(1)
1.java.io包中的字符流类-BufferedReader和BufferedWriter: BufferedReader:缓存的输入字符流; BufferedWriter:缓存的输出字符流; In ...
- 深入研究java.lang.ProcessBuilder类
深入研究java.lang.ProcessBuilder类 一.概述 ProcessBuilder类是J2SE 1.5在java.lang中新添加的一个新类,此类用于创建操作系统进程,它 ...
- asp.net 字符帮助类 类型转换类
/// <summary> /// 字符帮助类 /// </summary> public class StringHelper { private static readon ...
- javaio学习笔记-字符流类(2)
1.java.io包中的字符流类-FileReader和FileWriter: BufferedReader:缓存的输入字符流; BufferedWriter:缓存的输出字符流; FileReader ...
随机推荐
- VO、DTO、DO、PO的概念、区别和用处
转至:http://qixuejia.cnblogs.com/ 本篇文章主要讨论一下我们经常会用到的一些对象:VO.DTO.DO和PO. 由于不同的项目和开发人员有不同的命名习惯,这里我首先对上述的概 ...
- [New learn]SDWebImage框架的基本使用
代码:https://github.com/xufeng79x/SDWebImage 1.简介 SDWebImage是一个第三方框架,它能够帮助我们有效管理应用图片下载,沙盒保存和内存保存的任务.通过 ...
- Docker学习总结之跨主机进行link
原文来自: http://www.cnblogs.com/vikings-blog/p/4223462.html
- 一次cloudstack启动cloudstack-agent报错的处理过程
http://www.bubuko.com/infodetail-2397888.html
- 四十二 常用内建模块 collections
collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtuple 我们知道tuple可以表示不变集合,例如,一个点的二维坐标就可以表示成: >>> ...
- python模式匹配,提取指定字段
re匹配时分多行模式(re.M)与单行模式(rs.S),多行模式是每一行单独匹配,单行模式是把所有的行当成一行来匹配. 单行模式下.可以匹配换行符. ^$匹配所有字符 import re s='1_2 ...
- 错误:Eclipse老是出现 updating error reports database
Eclipse 火星版(Mars)一直出现 updating error reports database. Window--->Preferences--->General---> ...
- python 字符集转换-灰常慢
代码 def toUni (text): str = text try: charstyle = chardet.detect(text) # print 'confidence: ', charst ...
- Java中的冒泡排序(减少比较次数)
package yzhou.sort; import java.util.Arrays; public class BubbleSort { public static void main(Strin ...
- HDU 4891 The Great Pan
模拟题. #include<map> #include<set> #include<ctime> #include<cmath> #include< ...