C# 替换用户在句子中输入的单词,然后输出该句子,但使用经过审查/替换的单词

C# 替换用户在句子中输入的单词,然后输出该句子,但使用经过审查/替换的单词,c#,visual-studio-2015,C#,Visual Studio 2015,我的目标是替换出现在句子中的数组(由用户输入)中的单词。然后我想输出这句话,但替换版本是******,或者例如,censor将变成c****r static void CensorSelectedWords(string _sentence, int _wordCount) { string[] words = new string[_wordCount]; string[] splitSentence = new string[_wordCount]; for (int

我的目标是替换出现在句子中的数组(由用户输入)中的单词。然后我想输出这句话,但替换版本是******,或者例如,censor将变成c****r

static void CensorSelectedWords(string _sentence, int _wordCount)
{
    string[] words = new string[_wordCount];
    string[] splitSentence = new string[_wordCount];
    for (int i = 0; i < words.Length; i++)
    {
        Console.WriteLine("Type in the words you wish to be censored: ");
        words[i] = Console.ReadLine();
        splitSentence = _sentence.Split(' ', '.', '?', ',', '!');
        for (int j = 0; j < splitSentence.Length; j++)
            if (splitSentence[j] == words[i])
                splitSentence[j] = words[i].Replace(words[i], "*");
    }
    Console.WriteLine(splitSentence);
}
static void-censtorselectedwords(string\u-station,int\u-wordCount)
{
string[]words=新字符串[_wordCount];
字符串[]拆分句子=新字符串[_wordCount];
for(int i=0;i
你可以这样替换句子中的每个单词:

static void CensorSelectedWords(string _sentence, int _wordCount)
{
    string astr;
    string[] words = new string[_wordCount];
    string[] splitSentence = new string[_wordCount];
    for (int i = 0; i < words.Length; i++)
    {
        Console.WriteLine("Type in the words you wish to be censored: ");
        words[i] = Console.ReadLine();
        astr = "";
        for (int j = 0; j < words[I].Length; j++)
            astr += "*";
        _sentence = _sentence.Replace(word[i], astr);
    }
    Console.WriteLine(_sentence); 
}
static void-censtorselectedwords(string\u-station,int\u-wordCount)
{
弦式应力传感器;
string[]words=新字符串[_wordCount];
字符串[]拆分句子=新字符串[_wordCount];
for(int i=0;i
我对输出应该是什么有点困惑,但这里有一种方法。我已经将用经过审查的文本替换单词的功能提取到单独的函数中,以便可以独立修改

剩下的很简单-获取一个句子,获取一个经过审查的单词列表,然后将所有单词替换为句子中经过审查的对应单词并显示:

public static string HideAllButFirstAndLast(string word)
{
    if (word == null) return null;
    if (word.Length < 4) return new string('*', word.Length);
    return word[0] + new string('*', word.Length - 2) + word[word.Length - 1];
}

private static void Main()
{
    Console.WriteLine("Enter a sentence: ");
    var sentence = Console.ReadLine();

    Console.WriteLine("Enter a comma-separated list of censored words:");
    var censoredWords = Console.ReadLine()
        .Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries)
        .Select(word => word.Trim())
        .ToList();

    // Split sentence into words, replace words as needed, and join the words again
    sentence = string.Join(" ", sentence.Split().Select(word =>
        censoredWords.Contains(word, StringComparer.OrdinalIgnoreCase)
            ? HideAllButFirstAndLast(word)
            : word));

    Console.WriteLine(sentence);

    GetKeyFromUser("\n\nDone! Press any key to exit...");
}
public静态字符串hidealbutfirstandlast(字符串字)
{
if(word==null)返回null;
if(word.Length<4)返回新字符串(“*”,word.Length);
返回字[0]+新字符串('*',word.Length-2)+字[word.Length-1];
}
私有静态void Main()
{
Console.WriteLine(“输入一个句子:”);
var语句=Console.ReadLine();
WriteLine(“输入一个逗号分隔的删失单词列表:”);
var censoredWords=Console.ReadLine()
.Split(新[]{',},StringSplitOptions.RemoveEmptyEntries)
.Select(word=>word.Trim())
.ToList();
//将句子拆分成单词,根据需要替换单词,然后重新连接单词
句子=string.Join(“,句子.Split()。选择(word=>
包含(word、StringComparer.OrdinalIgnoreCase)
?隐藏第一个和最后一个(单词)
:字);
控制台。书写线(句子);
GetKeyFromUser(“\n\n完成!按任意键退出…”);
}
输出


可能是重复的我试过了,但是我正在努力让句子输出,但是用的是经过审查的单词。我将if循环中的单词[I]替换为拆分句子[j]。将最后一行更改为输出_句子。但是,它不会替换单词。我已经尝试过修改它,所以它会用适当数量的星号替换单词。我将不需要包括这个分裂的句子,我会吗?我编辑它添加正确的星号数。是的,你不必使用split,我想你可能会遇到大小写差异的问题。我已经解决了。我创建了一个名为censtered的新字符串,然后将该字符串替换为*长度与words数组中的当前元素相同。然后我把你的方法放在用新字符串“Censered”替换句子中的所有内容上@EricWarburton我想你遗漏了什么,它不会给出所需的结果。@AbdelAzizAbdelLatef哦,你是对的,我误用了Replace。它将是_句子=_句子.Replace(单词[i],删失的.PadLeft(单词[i].长度,'*');