0

Here I have some simple C# code;

Console.WriteLine("Enter Name");
var name = Console.ReadLine();
if (name == "ashley")
{
    Console.WriteLine("You entered: " + name);
}
Console.Read();`

If the user enters ashley it will display "You entered ashley". However if the user enters Ashley or AsHlEy it won't work. What do I need to add to this or how to format so it will ignore the case?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Skeeter
  • 173
  • 1
  • 1
  • 14

5 Answers5

4

Use string.Equals with an appropriate StringComparison

if (string.Equals(name, "ashley", StringComparison.CurrentCultureIgnoreCase))
{
   ...
}

If you know that the variable is not null you can also use

if (name.Equals("ashley", StringComparison.CurrentCultureIgnoreCase))
{
   ...
}

To answer your question in the comments, a do-while loop can be used to loop until the question is answered correctly. The below will loop until the user enters something other than ashley.

string name;
do
{
     Console.WriteLine("Enter Name");
     name = Console.ReadLine();
}
while (string.Equals(name, "ashley", StringComparison.CurrentCultureIgnoreCase));

You could combine this with a guard variable if you want different messaging:

string name;
bool nameIsCorrect = false;
do
{
     Console.WriteLine("Enter Name");
     name = Console.ReadLine();

     nameIsAshley = string.Equals(name, "ashley", StringComparison.CurrentCultureIgnoreCase);

     if (nameIsAshley)
     {
        Console.WriteLine("Stop entering 'ashley'");
     }
}
while (!nameIsAshley);
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • @user1951143 put it in a while loop (I'll update with an example) – tvanfosson Sep 08 '13 at 02:03
  • Thanks. I put this together. Console.WriteLine("Enter Name"); var name = Console.ReadLine(); if (String.Compare(name, "ashley", true) == 0) { Console.WriteLine("You entered: " + name); } else { Console.WriteLine("Quit messing around. Really what is the name?"); do { name = Console.ReadLine(); } while (string.Equals(name, "ashley", StringComparison.CurrentCultureIgnoreCase)); } – Skeeter Sep 08 '13 at 02:27
  • I notice one thing. If the name is entered wrong once it will prompt the user to enter the name again. If it is entered wrong twice, it completes the program anyway. It won't prompt a second time for the correct name. Not enough room to post the code I'm using. – Skeeter Sep 08 '13 at 02:34
3

String.Compare takes a boolean parameter which allows you to ignore casing during comparison:

Console.WriteLine("Enter Name");
var name = Console.ReadLine();

if (String.Compare(name, "ashley", true) == 0)
{
    Console.WriteLine("You entered: " + name);
}

Console.Read();
Alex
  • 34,899
  • 5
  • 77
  • 90
  • MSDN says that String.Compare should be used for sorting operations while string.Equals is used to determine if 2 string are equivalent. http://stackoverflow.com/a/44301/378280 – norepro Sep 08 '13 at 01:49
  • Thanks, no say I have an else clause if they enter a different name. So I want them to enter the correct name and continue with the original if statement. How do I do that? ` Console.WriteLine("Enter Name"); var name = Console.ReadLine(); if (String.Compare(name, "ashley", true) == 0) { Console.WriteLine("You entered: " + name); } else { Console.WriteLine("Quit messing around. Really what is the name?"); } Console.Read();` – Skeeter Sep 08 '13 at 02:04
  • @norepro sure, it would make sense to use `String.Compare` for sort operations and `String.Equals` for equivalency. Though, given `String.Compare` performs faster than `String.Equals` I'd lean towards using `String.Compare` ... http://blogs.msdn.com/b/kirillosenkov/archive/2010/09/22/what-s-faster-string-equals-or-string-compare.aspx – Alex Sep 08 '13 at 02:25
  • @Xander I'd take that with a grain of salt, for instance, it doesn't compare the static equals method with the static compare, it compares and instance method with a static method. I haven't tested it, but I would be surprised if there were any difference. – tvanfosson Sep 08 '13 at 02:31
  • @Xander interesting. I spun up a console app and copied that exact test and got these results: Equals Timer: 00:00:00.0000011 | Compare Timer: 00:00:00.0000015. If I were going to really test it, I'd throw in a large number of iterations. Cheers! :) – norepro Sep 08 '13 at 02:33
0

change this:

if (name == "ashley")

to this:

if (name.ToLower() == "ashley")
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • 2
    Because strings are immutable, this will create a new copy of the string so you pay the penalty of the copy + the comparison. Better to use an override of `Equals` that allows for insensitive comparison. Also, you need to be careful that `name` isn't null. – tvanfosson Sep 08 '13 at 01:38
0

Use ToLower like this:

Console.WriteLine("Enter Name");
var name = Console.ReadLine();
if (name.ToLower() == "ashley")
{
    Console.WriteLine("You entered: " + name);
}
Console.Read();`
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • Because strings are immutable, this will create a new copy of the string so you pay the penalty of the copy + the comparison. Better to use an override of `Equals` that allows for insensitive comparison. Also, you need to be careful that `name` isn't null. – tvanfosson Sep 08 '13 at 01:39
0

You can use the String.ToLower method

your test would be: if (name.ToLower() == "ashley")

Cedias
  • 904
  • 10
  • 19
  • 1
    Because strings are immutable, this will create a new copy of the string so you pay the penalty of the copy + the comparison. Better to use an override of `Equals` that allows for insensitive comparison. Also, you need to be careful that `name` isn't null. – tvanfosson Sep 08 '13 at 01:40
  • @tvanfosson That performance hit is tiny; probably immeasurable. Why encourage someone new to do premature optimizations? – Paul Phillips Sep 08 '13 at 01:45
  • 3
    @PaulPhillips it's not a premature optimization it's a bad habit to be avoided. Consider, `fooList.Any(f => f.ToLower() == "ashley");` – tvanfosson Sep 08 '13 at 01:47
  • @tvanfosson Talking about the penalty of the copy only matters if you're worried about the performance. Unless this is critical path stuff inside a deep loop, that simply isn't going to matter. – Paul Phillips Sep 08 '13 at 01:51
  • 2
    @PaulPhillips the point is there is a very simple (and correct) way to compare strings ignoring case that never incurs a performance penalty. It's best to get into the habit of using it and discourage the bad habit of making a copy then comparing. – tvanfosson Sep 08 '13 at 01:54