如何忽略大写和小写字母

How to neglect Capital and small alphabet in all

string usertype;
usertype = Console.ReadLine();

if (usertype== "Yahoo")
{ 
  Console.WriteLine("You typed Yahoo therefore we are now login to Yahoo Page");
  Console.ReadLine();
}

代码没有任何问题,除了:如果用户键入 Yahoo 然后它会显示答案。我想要用户;如果他输入 yahoo 那么答案应该是一样的。

string usertype;
usertype = Console.ReadLine();

if (string.Equals(usertype,"Yahoo",StringComparison.OrdinalIgnoreCase))
{ 
  Console.WriteLine("You typed Yahoo therefore we are now login to Yahoo Page");
  Console.ReadLine();
}

您可以使用具有类似代码的 String.ToLower() 方法:

string usertype;
usertype = Console.ReadLine();

if (usertype.ToLower() == "yahoo")
{ 
  Console.WriteLine("You typed Yahoo therefore we are now login to Yahoo Page");
}

不要使用“==”运算符进行比较,而是使用 String.Equals 方法。

如果您需要不区分大小写的比较,只需使用 System.StringComparison.OrdinalIgnoreCase StringComparison 枚举。

来自 MSDN 的描述:

basic ordinal comparison (System.StringComparison.Ordinal) is case-sensitive, which means that the two strings must match character for character: "and" does not equal "And" or "AND". A frequently-used variation is System.StringComparison.OrdinalIgnoreCase, which will match "and", "And", and "AND". StringComparison.OrdinalIgnoreCase is often used to compare file names, path names, network paths, and any other string whose value does not change based on the locale of the user's computer. For more information

可以找到更多信息here

在你的情况下,我会使用以下条件:

    if (usertype.Equals("yahoo", StringComparison.OrdinalIgnoreCase))
        //then do whatever you want...

另一个选项是将所有内容小写,但首选第一个选项。

if (usertype.ToLower() == "yahoo")
    //then do whatever you want...