从字符串C#中删除一个单词

时间:2017-09-22 09:48:44

标签: c# string

我正在进行一次AI会话,我试图做到这一点,以便它询问用户他们是如何做的。我没有输入不同可能答案的负载,而是试图通过删除单词"而感谢"在答复中,以便减少可能性。因此,不必创建响应"好,"非常感谢","伟大"并且非常感谢"如果感谢这个词在回复中,它只会将其删除,并且只需要寻找"好"或者"伟大的" 我从另一个网站上删除了部分,但我不认为我正确使用它。 到目前为止,这是我的代码。

static void Main(string[] args) 
{
    Console.WriteLine("What is your name?");
    string name;
    name = Console.ReadLine();
    Console.WriteLine("Oh hi {0}! Nice to meet you.", name);

    Console.WriteLine("How are you doing today?");
    string mood;
    mood = Console.ReadLine();
    mood = mood.ToLower();
    int index1 = mood.IndexOf("thanks");
    if (index1 != -1)
    {
        string mood2 = mood.Remove(index1);
    }
    else
    {

    }
    if (mood2 == "good" || mood2== "alright" || mood2 == "great")
    {
        Console.WriteLine("Good to hear that!");
    }
    else
    {
        Console.WriteLine("Ah well");
    }
}

感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

Remove方法的使用实际上是正确的。根据{{​​3}}它:

  

返回一个新字符串,其中当前实例中从指定位置开始并持续到最后一个位置的所有字符都已被删除。

您的代码应该为您提供编译器错误,即当前上下文中不存在变量mood2。将声明移出if-scope:

string mood2 = "";
if (index1 != -1)
{
    mood2 = mood.Remove(index1);
}

或只是覆盖原始mood变量并将其用于进一步比较:

if (index1 != -1)
{
    mood = mood.Remove(index1);
}
else
{

}
if (mood == "good" || mood == "alright" || mood == "great")
{
    Console.WriteLine("Good to hear that!");
}
else
{
    Console.WriteLine("Ah well");
}

更简单的检查方法是使用documentation方法。它将检查您的字符串是否包含特定单词。因此,您可以跳过删除部分,只需像这样检查:

string mood = Console.ReadLine().ToLower();

if (mood.Contains("good") || mood.Contains("alright") || mood.Contains("great"))
{
    Console.WriteLine("Good to hear that!");
}
else
{
    Console.WriteLine("Ah well");
}

编辑:

如果您想检查否定,可以采用相同的方式:

if (mood.Contains("good") || mood.Contains("alright") || mood.Contains("great"))
{
    if(mood.Contains("not")
    {
        Console.WriteLine("Ah well");
    }
    else
    { 
        Console.WriteLine("Good to hear that!");
    }
}
else
{
    Console.WriteLine("Ah well");
}

你需要做的就是对“坏”,“可怕”,“可怕”等情绪的不良描述。

答案 1 :(得分:1)

你可以像以前一样删除一个单词,但字符串没有初始化(如Mong Zhu所述),所以你需要初始化它然后它会起作用:

string mood2 = "";// < Correct if this is added
if (index1 != -1)
{
    mood2 = mood.Remove(index1);
}

清洁方法:

但是你真的需要删除&#34;感谢&#34;部分原因是因为它只是检查该字符串中的其他内容(mood2 == "good" || mood2== "alright" || mood2 == "great")您可以检查您的字符串是否包含其中一个单词,代码将变得更短:

string[] moods = { "good", "alright", "great" };
string name, chosenMood;

Console.WriteLine( "What's your name?" );
name = Console.ReadLine();
Console.WriteLine( "How are you doing today?" );
chosenMood = Console.ReadLine();

if( moods.Any(mood => chosenMood.ToLower().Contains(mood)) )
{
    Console.WriteLine( "Good to hear that!" );
}
else
{
    Console.WriteLine( "Ah well" );
}
Console.ReadKey();

通过这种方式,您还可以键入类似“非常好!”的内容!&#39;它仍然可以工作。

如果您回答“并不完美”这样的话会产生误报:(&#39;

答案 2 :(得分:0)

为了您的目标,我会检查用户的mood是否以您的感觉良好开始#34;词语的

var goodAnswers = new string[] {"good", "alright", "great"};
if (goodAnswers.Any(okAnswer => mood.ToLower().StartsWith(okAnswer)))
{
    Console.WriteLine("Good to hear that!");
}
else
{
    Console.WriteLine("Ah well");
}