Search code examples
c#multidimensional-arrayignore-case

Having trouble with IgnoreCase


Hi im having trouble on putting an IgnoreCase to these codes

Console.WriteLine("Select a seat that you want to ocupy");
            string UserInput = Console.ReadLine();
            //replacing array values with X
            for (int row = Arr.GetLowerBound(0); row <= Arr.GetUpperBound(0); ++row)
            {
                for (int column = Arr.GetLowerBound(1); column <= Arr.GetUpperBound(1); ++column)                    
                    if (Arr[row, column].Contains(UserInput))                       
                        {
                            Arr[row, column] = " X ";
                        }                                                                  
            }

I'm replacing an 2d array value with "X" via UserInput Here's my array

string[,] Arr = new string[,]
{{"A1" , " A2" , " A3" , " A4" , " A5"},
{"B1" , " B2" , " B3" , " B4" , " B5"}};

Solution

  • String.Contains does not have the ability to specify a culture or case comparison. Because of this, you will probably want to use ToLowerInvariant.

    For example:

    string UserInput = Console.ReadLine().ToLowerInvariant();
    
    ...
    
    if (Arr[row, column].ToLowerInvariant().Contains(UserInput))