我想知道如何在文本更改事件中自动过滤掉 richtextbox
中的坏词。我正在通过使用 ip 在计算机之间创建连接来开发本地聊天软件,但我需要对其进行过滤,例如
Richtextbox.text = "oh s***";
Richtextbox
将弹出一个消息框提醒用户并禁用输入 5 秒,然后再次启用。
最佳答案
有趣的问题!我想,类似的东西:
using System.Text.RegularExpressions;
...
HashSet<String> badWords = new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
"bad",
"words",
};
Boolean result = YourRichTextBox
.Lines
.Any(line => Regex
.Split(line, @"\W")
.Any(word => badWords.Contains(word)));
请注意,坏词可以从大写字母、大写字母等开始。另一个困难是我们必须检测“BAD!”
,但不能检测“baddy”
.
要提醒用户只需将代码放入TextChanged
事件处理:
private void YourRichTextBox_TextChanged(object sender, EventArgs e) {
RichTextBox YourRichTextBox = sender as RichTextBox;
Boolean result = ... // See code above
if (result) {
MessageBox.Show("You must not be that rude!", Text, MessageBoxButtons.OK);
...
}
}
关于c# - 如何从不良词中过滤出richtextbox?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32334864/