用不区分大小写的干净单词替换文本 c#

replacing text with cleaned word case insensitive c#

我有一个坏词列表,如果在文本字符串中找到,将用干净的词替换。 例如。 badwords{woof} is replaced by w$$f

但目前仅当数组列表与句子中的匹配词大小写相同时才有效。

var badWords = new List<string>{"woof", "meow"}

var string = "I have a cat named meow and a dog name Woof." 应该变成==="I have a cat named m$$w and a dog name W$$f"

public string CensorText(string text)
    {
        if (string.IsNullOrWhiteSpace(text))
        {
            return text;
        }
        foreach (string word in CensoredWords)
        {
            text = text.Replace(word, WordCleaner(word));
        }
        return text;
    }

    private static string WordCleaner(string wordToClean)
    {
        string firstChar = wordToClean.Substring(0,1);
        string lastChar = wordToClean.Substring(wordToClean.Length - 1);
        string centerHash = new string('$', wordToClean.Length-2);

        return string.Concat(firstChar, centerHash, lastChar);         
    }

如何让它在遍历单词和清理单词时不区分大小写。答案越简单越好。

尝试替换:

text = text.Replace(word, WordCleaner(word));

text = text.Replace(word.ToLower(), WordCleaner(word));

这会将任何大写字母转换为小写字母。

编辑

我意识到我把错误的变量变成了小写。

变化:

public string CensorText(string text)
{

收件人:

public string CensorText(string text)
{
    text = text.ToLower();

编辑 2

要保留原句,改删词,改用re会方便很多。首先,将您的文件恢复到问题中的状态。

现在替换:

text = text.Replace(word, WordCleaner(word));

与:

text = regex.replace(text,word,WordCleaner(word),RegexOptions.Ignorecase);

这是一个您可以使用的简单选项。
好处是你不关心哪个单词是小写的,它适用于任何一种情况。请注意,比较 returns 一个 int,因此我们检查它是否为 0 是否匹配。

string input = "the Woof is on Fire, we don't need no bucket, leT the ...";
string[] bad_words = new string[] {"woof","fire","BucKet", "Let"};

foreach (var word in input.Split(' ')) {
    if (bad_words.Any( b => String.Compare( word, b // Following line does what you want:
                                          , StringComparison.OrdinalIgnoreCase) == 0))
        Console.Write(WordCleaner(word));
    else
        Console.Write(word);
}

输出:

the W$$f is on F$$e  we don't need no b$$$$t  l$T the ... 

我觉得不错。请注意,如果您在 space 上拆分,紧随其后的逗号单词会将逗号作为单词

的一部分