-2
string usertype;
usertype = Console.ReadLine();

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

Nothing wrong with t he code except: If user types Yahoo then it shows answer. I want user; if he types yahoo then answer should be the same.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • 1
    you can use ``string.Equals(usertype,"Yahoo",StringComparison.OrdinalIgnoreCase)`` it will compare without considering case of string – Ehsan Sajjad Mar 20 '16 at 08:20
  • string usertype; usertype = Console.ReadLine(); string.Compare(usertype,"Yahoo",StringComparison.OrdinalIgnoreCase); if (usertype== "Yahoo") { Console.WriteLine("You typed Yahoo therefore we are now login to Yahoo Page"); Console.ReadLine(); } else { Console.WriteLine("You didnt type Yahoo"); Console.Read(); } } Nope; it doesn't work. – Syed Irfan Naseer Mar 20 '16 at 08:23
  • you need to chane it in if: ``if(string.Equals(usertype,"Yahoo",StringComparison.OrdinalIgnoreCase)){ Console.WriteLine("You typed Yahoo therefore we are now login to Yahoo Page"); Console.ReadLine(); } else { Console.WriteLine("You didnt type Yahoo"); Console.Read(); }`` – Ehsan Sajjad Mar 20 '16 at 08:25

3 Answers3

2
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();
}
0

You can use String.ToLower() method with a similar code to this:

string usertype;
usertype = Console.ReadLine();

if (usertype.ToLower() == "yahoo")
{ 
  Console.WriteLine("You typed Yahoo therefore we are now login to Yahoo Page");
}
S.Dav
  • 2,436
  • 16
  • 22
0

Instead of comparing using "==" operator, use String.Equals method.

If you need a case-insensitive comparison just use System.StringComparison.OrdinalIgnoreCase StringComparison enum.

Description from 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

Further info can be found here

In your case I would use the following condition:

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

Another option is to lowercase everything but the first option is preferred.

if (usertype.ToLower() == "yahoo")
    //then do whatever you want...
Ofir
  • 5,049
  • 5
  • 36
  • 61
  • Thanks to all! if(string.Equals(usertype,"Yahoo",StringComparison.OrdinalIgnoreCase)){ Console.WriteLine("You typed Yahoo therefore we are now login to Yahoo Page"); Console.ReadLine(); } else { Console.WriteLine("You didnt type Yahoo"); Console.Read(); } Thats worked! – Syed Irfan Naseer Mar 20 '16 at 08:54