0

I have

public readonly IEnumerable<string> QuestionStrings = new List<string>
        {
           // okay?,are you okay?,what to do?
        };

I am reading input from user and if the input matches one of the questions listed above, I want to send the user a specific message .

My problem is that I want the comparison to be case insensitive and question mark insensitive .

So if the user enters okay?, Okay?, okAy or okay I want to treat all these messages the same and send the same specific message to user

I was able to compare the string regarding case insensitive

QuestionStrings.Contains(userInput, StringComparer.OrdinalIgnoreCase);

but I am couldn't find out a way to ignore the question mark

Any way to do that? "other than checking the input from user if it contains ? or not at the end"

Amin Golmahalleh
  • 3,585
  • 2
  • 23
  • 36
Rex
  • 147
  • 3
  • 11

4 Answers4

1

Instead of ignoring them, you can remove them in the first place in your list, and remove them from the input string when comparing:

public readonly IEnumerable<string> QuestionStrings = new List<string>
{
   "okay",
   "are you okay",
   "what to do"
};

...

QuestionStrings.Contains(userInput.Replace('?', ''), StringComparer.OrdinalIgnoreCase);

Another option, if you insist on keeping the question marks in the list, is to use Any instead of Contains - that will allow you to use a lambda expression to perform the comparison:

QuestionStrings.Any(s => s
    .Replace('?', '')
    .Equals(userInput.Replace('?', ''), StringComparison.OrdinalIgnoreCase));
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • Thanks a lot. It worked. but is there any other way to do it? maybe a regex? cause I might need to use this list including the question marks in another class – Rex May 06 '20 at 08:47
  • I've edited my answer to include another option that lets you keep the question marks in the list. You could use a regular expression instead of replace, but I don't think it will make much of a difference. – Zohar Peled May 06 '20 at 09:23
1

A flexible and expressive approach to the code would be to define your requirements with a a list of regular expressions (possibly stored in a resource table or database), then translate those into runtime logic directly.

Initialization:

//In a practical application you would load this from a resource or database
List<string> QuestionStrings = new List<string>
{
   "^okay.$",          //Can appear anywhere in string with or without question mark
   "are you okay.",    //Must be the entire string, with or without question mark
   "^what to do\?"     //Question mark is required
};

var regexs = QuestionStrings.Select( s => new RegEx(s) ).ToList();

Then, once you have user input, check for a match:

var match = regexs.Any( x => x.IsMatch( userInput ) );
John Wu
  • 50,556
  • 8
  • 44
  • 80
0

You can try this:

QuestionStrings.Contains(userInput.Replace("?","").Trim(), StringComparer.OrdinalIgnoreCase);
Jamil
  • 830
  • 2
  • 15
  • 34
  • Unless you remove the question marks from the strings in `QuestionStrings`, that will never give a correct answer... – Zohar Peled May 06 '20 at 08:11
0

You could use what is mentionned here : https://stackoverflow.com/a/368850/8233385 The purpose is to parse every char from the string in order to only take a to z chars

Kirjava
  • 226
  • 1
  • 11
  • Please don't link to an answer. Instead, summarize the contents of the answer here and be sure to [give proper attribution](https://stackoverflow.blog/2009/06/25/attribution-required/). – Heretic Monkey May 06 '20 at 18:01