首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >限制用户输入某些语言。

限制用户输入某些语言。
EN

Stack Overflow用户
提问于 2012-07-15 01:08:49
回答 1查看 213关注 0票数 1

我正在创建一个网站,其中包括一个评论区的用户。例如留言簿或产品评论。我想限制用户在评论区发布不适当的语言。例如:粗俗。

如果用户输入任何粗俗的内容,字符将被替换为*。*示例-从愚蠢到s ** *。

我一直在相关网站上搜索,但没有结果。在这方面的建议或教程将非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-15 03:17:52

没有办法完全阻止“坏语言”的使用,但你可以尝试通过创建一个每行都包含一个坏单词的文本文件来阻止它。然后将文件中的单词列表加载到程序中的List<String>中。为此,您可以执行以下操作:

代码语言:javascript
复制
// The list of swear words
List<string> swearWords = new List<string>();

private void GetSwearWords()
{
    // Get the path to the file that has the swear words list
    string path = <File Path>;

    // Open the text file
    TextReader reader = new StreamReader(path);

    // Loop through each line in the file.
    string line = "";
    while ((line = reader.ReadLine()) != null)
    {
       // Lower cases word and removes whitespaces
       string word = line.Trim().ToLower();

       // Adds the word to the list
       swearWords.Add(word);
    }
}

然后,要确定字符串是否包含这些坏词之一,请执行以下操作:

代码语言:javascript
复制
private bool HasSwearWord(string text)
{
    // Splits words, removes whitespace and any punctuation
    string[] wordArray = Regex.Split(text, @"\W+");

    // Check if any word in the string is a swear word
    foreach (string word in wordArray)
    {
        if (swearWords.Contains(word.ToLower()))
        {
            return true;
        }
    }
    return false;
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11485726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档