C# 如何过滤richtextbox中的脏话?
我想知道如何在文本更改事件中自动过滤C# 如何过滤richtextbox中的脏话?,c#,.net,C#,.net,我想知道如何在文本更改事件中自动过滤richtextbox。我正在开发一个本地聊天软件,使用ip在计算机之间建立连接,但我需要对其进行过滤,例如 Richtextbox.text = "oh s***"; Richtextbox将弹出一个消息框,提醒用户并禁用输入5秒钟,然后再次启用。我认为这个问题有点宽泛,但您可能可以使用Linq: List<string> badWords = new List<string> { "bad", "words", "here" };
richtextbox
。我正在开发一个本地聊天软件,使用ip在计算机之间建立连接,但我需要对其进行过滤,例如
Richtextbox.text = "oh s***";
Richtextbox
将弹出一个消息框,提醒用户并禁用输入5秒钟,然后再次启用。我认为这个问题有点宽泛,但您可能可以使用Linq:
List<string> badWords = new List<string> { "bad", "words", "here" };
string myString = "This string contains a bad word";
bool badWordInString = badWords.Any(myString.Contains);
List baddwords=新列表{“bad”,“words”,“here”};
string myString=“此字符串包含一个坏单词”;
bool badWordInString=badWords.Any(myString.Contains);
如果myString
包含列表中的任何坏单词,则badWordInString
将为true
然后,您可以使用text replace将有问题的单词替换为经过审查的替换词
问题是,以这种方式进行审查并不能解释诸如“坏”这个词在“坏”这个词中的含义。您可能希望允许使用baddy,但这并不坏,但由于这是在文本更改事件处理程序中发生的,因此您永远无法键入baddy
一个更好的解决方案是在发送文本后对其进行审查,寻找单词边界,修剪标点符号,忽略大小写,并检查整个单词是否匹配
- 为了进行测试,您需要硬编码一些单词
有趣的问题!我猜是这样的:
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)));
我必须在我的项目中实现这一点,我想我应该分享我的代码。我创建了一个文本文件并将其存储在网站中,因此可以轻松地对其进行修改,而无需重新编译或更改web.config设置 这样做的一个好方法是在按钮submit上执行此操作,因为您使用的是RTE。我想说,在按钮提交之前,使用ajax检查它是否包含“脏话”,这样您就不必回发,但看起来您使用的是Win表单,这就是MVC。但是你可以了解情况 我在这个网站上用了英语和西班牙语的“脏话” 文本文件位于/Content文件夹中(在我的情况下) 如果您可以使用(或者如果其他人愿意),这里有ajax Api方法-您也可以将其放入按钮提交事件中
[HttpPost] // <--- remove if not using Api
public string CheckForBadWords(string str)
{
string badWords = string.Empty;
var badWordsResult = Global.CheckForBadWords(str);
if (badWordsResult.Length > 0)
{
badWords = string.Join(", ", badWordsResult);
}
return badWords;
}
这是StackOverflow,您尝试了什么?编辑您的帖子以显示您的代码,不要将其作为注释发布。这就是说,只需编辑一个不允许使用的单词列表,并可能将您的文本框拆分到一个空格中,看看它是否包含任何不好的内容。我在移动设备中,我不能:(请编辑它:)该条件将永远不会计算为真。您将一个值与它自身进行比较,然后在其上附加一些内容--更改值。它们永远不会是一样的。我正在检查请等待结果:)需要更多说明在richtextbox或创建linq查询中将此代码发布到何处自从我回答问题后,您已经编辑了该问题,因此我需要将其更新为筛选器。这只会让您知道字符串中是否有一个坏单词。@AhmedTammaa,可能在textchanged事件中有此代码。是的,在text changed事件中。用
Richtextbox.TextmyString
切换出myString
我在这一行出现错误…布尔结果=YourRichTextBox.Lines.Any(line=>line.Regex.Split(line,@“\w”).Any(word=>baddwords.Contains(word))@Ahmed Tammaa:使用System.Text.RegularExpressions添加
to others Using and where is the alert how to pop the message box if the user Using bad Words,,please upvote my question,,错误仍然存在,没有修复抱歉:(你能帮忙吗?@Ahmed Tamma:我的2010 VS副本(不过我有终极版)编译代码。使用System.Linq;
在其他中使用获得了?您能回答它吗?如果您发现它与原始问题重复,则原始问题没有得到任何答案,请回答其中一个:)
$('#form-ID').on('click', 'button[type="submit"]', function (e) {
var badWords = '',
str = $('#form-ID').find('textarea').val();
$.ajax({
url: '/YourAPI/CheckForBadWords?str=' + str,
type: 'POST',
dataType: 'json',
data: '',
async: false,
contentType: 'application/json; charset=utf-8',
complete: function (data) {
badWords = data.responseText;
}
});
if (badWords != '') {
console.log('oh no --- ' + badWords)
e.preventDefault();
return false;
}
});
[HttpPost] // <--- remove if not using Api
public string CheckForBadWords(string str)
{
string badWords = string.Empty;
var badWordsResult = Global.CheckForBadWords(str);
if (badWordsResult.Length > 0)
{
badWords = string.Join(", ", badWordsResult);
}
return badWords;
}
public static class Global
{
/// <summary>
/// Returns a list of bad words found in the string based
/// on spanish and english "bad words"
/// </summary>
/// <param name="str">the string to check</param>
/// <returns>list of bad words found in string (if any)</returns>
public static string[] CheckForBadWords(string str)
{
var badWords = GetBadWords();
var badWordsCaught = new List<string>();
if (badWords.Any(str.ToLower().Contains))
{
badWordsCaught = badWords.Where(x => str.Contains(x)).ToList();
}
return badWordsCaught.ToArray();
}
/// <summary>
/// Retrieves a list of "bad words" from the text file. Words include
/// both spanish and english
/// </summary>
/// <returns>strings of bad words</returns>
private static List<string> GetBadWords()
{
var badWords = new List<string>();
string fileName = string.Format("{0}/Content/InvalidWords.txt", AppDomain.CurrentDomain.BaseDirectory);
if (System.IO.File.Exists(fileName))
{
badWords = System.IO.File.ReadAllLines(fileName).ToList();
}
return badWords.ConvertAll(x => x.ToLower());
}
}
var badWords = '',
str = stringHERE;
$.ajax({
url: '/YourApiController/CheckForBadWords',
type: 'POST',
dataType: 'json',
data: JSON.stringify({ str: str }),
async: false,
contentType: 'application/json; charset=utf-8',
complete: function (data) {
badWords = data.responseText;
}
});