1

I'm making a text game, and I have one method for getting the input from the player and another for processing that input. However, it only works if the player types the command all in lowercase. I want it to ignore case.

        public string GetInput()
    {
        var Test = true;

        while (Test)
        {
            response = Console.ReadLine();
            if (validWords.Contains(response))
            {
                Test = false;
                ProcessInput(response);
            }
            else
            {
                Console.WriteLine("I'm sorry, I do not understand.");
            }
        }
        return response;
    }
public void ProcessInput(string response)
    {
        switch (response)
        { //Switch statements for responses here
        }
    }

I've tried using a few other responses I've found here, but they all still only work with lowercase input(using LINQ, string.IndexOf/Equals/etc.). Ideas?

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
Hub_Batch
  • 47
  • 4
  • 2
    What is the content of validWords? If these are all lower case strings then just convert the user input to a lower case word and then run the search. IE _response = Console.ReadLine().ToLower();_ – Steve Mar 21 '17 at 12:52
  • They are all lowercase-- where do I put ToLower? – Hub_Batch Mar 21 '17 at 12:55
  • Just convert the input to lower case. No other changed needed then. (in particular inside the switch statement) – Steve Mar 21 '17 at 12:58

5 Answers5

3

Use string comparer which ignores string case as second parameter for Contains method:

validWords.Contains(response, StringComparer.OrdinalIgnoreCase)
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
2

You can add .ToLower() after the readline, as followed:

response = Console.ReadLine().ToLower();

Everything read in from the console will be in lowercase.

You can read more about the ToLower method in the MSDN documentation.

Furthermore, also see the following question if you expect input of certain cultures: string.ToLower() and string.ToLowerInvariant().

Community
  • 1
  • 1
PJvG
  • 1,310
  • 3
  • 16
  • 33
2

You can make every input you receive, Lower-Case, using ToLower().

Example:

string response = Console.ReadLine().ToLower();
Rafael
  • 727
  • 13
  • 30
1

Change the text you have to lower using .ToLower().

ajarianaka
  • 38
  • 1
  • 6
-1

You're currently using if (validWords.Contains(response)) to check whether the user's response is contained within your validWords list. (I'm assuming).

What you can do is use the following LINQ expression:

if (validWords.Where(w => w.ToUpper().Equals(response.ToUpper())).Count() == 1)

This validates against both upper case strings.

Nathangrad
  • 1,426
  • 10
  • 25
  • 1
    This will work but won't be as efficient as simply converting console input to lower case (1 operation). Your solution unnecessarily executes ToUpper() on every valid word (1 operation on console input + n operations on validWords). – J. Tuc Mar 21 '17 at 13:06