【问题标题】:Having trouble with IgnoreCaseIgnoreCase 遇到问题
【发布时间】:2014-11-30 08:28:11
【问题描述】:

您好,我在将 IgnoreCase 放入这些代码时遇到了麻烦

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

我通过 UserInput 将二维数组值替换为“X” 这是我的数组

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

【问题讨论】:

  • 请多解释一下您将该代码放在哪里以及您收到了什么异常
  • 我正在尝试在这一行添加一个 IgnoreCase if (Arr[row, column].Contains(UserInput))
  • 请更新您的错误代码
  • 你得到什么错误?
  • Arr[row, column].ToLower().Contains(UserInput.ToLower())

标签: c# multidimensional-array ignore-case


【解决方案1】:

String.Contains 无法指定区域性或大小写比较。因此,您可能希望使用ToLowerInvariant。

例如:

string UserInput = Console.ReadLine().ToLowerInvariant();

...

if (Arr[row, column].ToLowerInvariant().Contains(UserInput))

【讨论】:

    【解决方案2】:

    你应该改用这个:

    Arr[row, column].Contains(UserInput.ToUpper())
    

    使用 ToUpper() 将始终比较输入的大写版本,无论大小写如何。只要确保数组是你提到的“即全部大写”

    【讨论】:

      猜你喜欢
      • 2021-08-09
      • 2017-05-02
      • 2021-02-01
      • 2012-10-31
      • 2019-07-26
      • 2018-01-07
      • 2016-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多