简体   繁体   中英

Removing one word from a string C#

I'm making a little conversation AI, and I'm trying to make it so that it asks the user how they are. Instead of typing loads of different possible answers I'm trying to narrow it down a bit by removing the word "thanks" in the reply so that there are less possibilities. So instead of having to create the responses "good, "good thanks", "great" and "great thanks", if the word thanks is in the reply it will just remove it and will only have to look for "good" or "great" I got the removing the word part from another website but I don't think I am using it correctly. Here is my code so far.

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");
    }
}

Thanks for the help.

The usage of the Remove method is actually correct. According to the documentation it :

Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted.

your code should give you a compiler error that the variable mood2 does not exist in the current context. Either move the declaration out of the if-scope:

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

or simply overwrite the original mood variable and use it for further comparison:

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");
}

a much easier way of checking would be to use the String.Contains method. It will check whether you string contains a specific word. So you can skip the removing part an simply check like this:

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");
}

EDIT:

If you want to check for negation you can do it the same way:

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");
}

the same you need to do for the bad descriptions of the mood like "bad", "terrible", "horrible" ect.

You can remove a word like you did before but the string wasn't initialized (as Mong Zhu said) so you need to initialize it and then it will work:

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

Cleaner method:

But do you actually need to remove the "thanks" part because as it stand you only check what else is in that string ( mood2 == "good" || mood2== "alright" || mood2 == "great" ) You could just check if your string contains one of those words, the code would become a lot shorter:

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();

This way you can also type something like 'Pretty darn good!' and it will still work.

It will give false positives if you answer something like 'Not good at all :('

For your goal, I would just check that user's mood starts with your "feeling good" words.

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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM