unity c 非法字元(髒詞)檢測

2021-09-02 22:27:10 字數 3901 閱讀 5159

專案中非法字元檢測是必須的,聊天系統不遮蔽各種不文明用語

先說說我的原理吧

1.讀取非法字元表,把相同的首字元歸類到字典,類似新華字典那樣

2.然後把輸入的字串,乙個個字元找對應的首字元字典,遍歷首字元字典,在當前字元後面擷取對應的字元長度得到的字串然後比較,如果字串相同則認為有非法字元

下面是測試結果

下面為完整**,有注釋應該比較容易看懂

using system;

using system.collections.generic;

using system.linq;

using system.text;

using unityengine;

///

public class filterword

private string m_allfilterword = string.empty;

/// /// 詞庫路徑

///

public string allfilterword

set

}/// /// 記憶體詞典

///

private wordgroup memorylexicon = new wordgroup[(int)char.maxvalue];

private string sourcttext = string.empty;

private bool m_isinitalize = false;

/// /// 檢測源

///

public string sourcetext

set

}/// /// 檢測源游標

///

int cursor = 0;

/// /// 匹配成功後偏移量

///

int wordlenght = 0;

/// /// 檢測詞游標

///

int nextcursor = 0;

private listillegalwords = new list();

/// /// 檢測到的非法詞集

///

public listillegalwords

}/// /// 判斷是否是中文

///

///

///

private bool ischs(char character)

/// /// 判斷是否是數字

///

///

///

private bool isnum(char character)

/// /// 判斷是否是字母

///

///

///

private bool isalphabet(char character)

/// /// 轉半形小寫的函式(dbc case)

///

/// 任意字串

/// 半形字串

//////全形空格為12288,半形空格為32

///其他字元半形(33-126)與全形(65281-65374)的對應關係是:均相差65248

///private string todbc(string input)

if (c[i] > 65280 && c[i] < 65375)

c[i] = (char)(c[i] - 65248);

}return new string(c).tolower();

}/// /// 載入記憶體詞庫

///

public void loaddictionary()

m_isinitalize = true;

listwordlist = new list();

array.clear(memorylexicon, 0, memorylexicon.length);

string words = allfilterword.split('\n');

foreach (string word in words)

comparisoncmp = delegate (string key1, string key2)

;wordlist.sort(cmp);

for (int i = wordlist.count - 1; i > 0; i--)

}foreach (var word in wordlist)

wordgroup group = memorylexicon[word[0]];

if (group == null)

group.add(word.substring(1));}}

/// /// 檢測

///

///

///

private bool check(string blackword)

else

if ((int)blackword[i] == (int)tempstr[nextcursor + offset])

if (i >= blackword.length - 1)

else

}else}}

else}}

else

}nextcursor = nextcursor + 1 + offset;

wordlenght++;

}return found;

}/// /// 查詢並替換

///

///

public string filter(char replacechar)

illegalwords.add(blackword);

cursor = cursor + wordlenght;

i = i + wordlenght;}}

}cursor++;

}return new string(tempstring);

}else

}}/// /// 具有相同首字元的片語集合

///

class wordgroup

/// /// 新增詞

///

///

public void add(string word)

/// /// 獲取總數

///

///

public int count()

/// /// 根據下標獲取詞

///

///

///

public string getword(int index)

}

下面是抽出乙個統一方法來呼叫檢測

主要兩個方法

1.檢測是否有非法字元,返回bool

2.把非法字元轉成*號,返回string

using system.collections;

using system.collections.generic;

using unityengine;

public class systemutil

/// /// 把非法字元變成*號

///

///

///

public static string filter(string str)

public static filterword filterword

return m_filterword;}}

private static filterword m_filterword;

}

過濾非法字元

using system using system.componentmodel using system.collections using system.diagnostics using system.data using system.data.sqlclient using system....

過濾非法字元

再度改進,在髒字可能存在的情況下,例如出現了多個髒字前length 1部分時,效能相比http www.cnblogs.com xingd archive 2008 01 31 1060425.html 中描述的又提公升了300 400 直接貼出全部 了,通過新增的乙個byte char.maxva...

xml中非法字元

今使用jdom從資料庫讀取資料生成xml檔案,然後再解析xml檔案時,總是出現0x0,非法字元,經過搜尋,問題原因及解決方法如下 原因 xml中需要過濾的字元分為兩類,一類是不允許出現在xml中的字元,這些字元不在xml的定義範圍之內。另一 類是xml自身要使用的字元,如果內容中有這些字元則需被替換...