-4

So, in my quest to learn C#, I am attempting to create an interactive story that changes based on some of the input that the user had submitted. If the user types in "Bobby" in this case, the narrator begins to talk like Hank Hill. However, with how it's programmed, the input is case sensitive.

I have tried one thing suggestion that I saw which was to format the if statement as:

if (boyName.ToUpper() == "Bobby")

But that did not trigger the if command with different letter cases

 Console.WriteLine($"{beginning} \n What was the boy's name?");
 boyName = Console.ReadLine();
  if (boyName == "Bobby")
   {
   Console.WriteLine("That boy ain\'t right, I tell ya what... ");
   Console.ReadKey();
   Console.WriteLine($"{boyName} boy dang climbed a big ol' tree..."); 
   Console.ReadKey();
   }

   else
    {
    Console.WriteLine($"The kid named {boyName} climbed a tree...");
    Console.ReadKey();
    }

I expect to have a line of code that will trigger the if condition no matter the case. However, everything I tried has not changed that. It needs to be specifically "Bobby" or it will trigger the else condition

DaimonCide
  • 11
  • 1
  • 4
  • 3
    https://learn.microsoft.com/en-us/dotnet/csharp/how-to/compare-strings – UnholySheep Apr 18 '19 at 17:25
  • 3
    `boyName.ToUpper() == "BOBBY"` – L.B Apr 18 '19 at 17:27
  • Welcome to StackOverFlow @DaimonCide, Maybe you can try and uppercase both sides of your if statement like `if (boyName.ToUpper() == "Bobby".ToUpper())` or lowercase both sides of your if `if (boyName.ToLower() == "Bobby".ToLower())` – Tendai Mare Apr 18 '19 at 17:31
  • Hi. Maybe you know .ToUpper function incorrectly. This function does not make the string camel-case, it makes all letters capitalized. – serdar Apr 18 '19 at 17:37
  • Possible duplicate of [How can I do a case insensitive string comparison?](https://stackoverflow.com/questions/3121957/how-can-i-do-a-case-insensitive-string-comparison) – stannius Apr 18 '19 at 17:51

2 Answers2

4

It's technically better to use a case-insensitive comparison rather than changing the case of the strings being compared, because ToUpper() will not always work as expected (from a comparison point of view) with all languages (alphabets). See "the Turkish 'i'" section in this article on case folding for more info.

To solve your issue without modifying the original strings, you can use the String.Equals method, which takes arguments for the strings to compare as well as one that specifies the type of comparison to use.

Therefore your code might look like this:

if (string.Equals(boyName, "Bobby", StringComparison.OrdinalIgnoreCase))

Or you could use the instance method version, which is a little shorter:

if (boyName.Equals("Bobby", StringComparison.OrdinalIgnoreCase))
Rufus L
  • 36,127
  • 5
  • 30
  • 43
0

You should try

if (boyName.ToUpper() == "Bobby".ToUpper())
Paulo Campez
  • 702
  • 1
  • 8
  • 24
  • Thank you. That solved my issue. I rarely like to do things without knowing why it works, so why does that work? – DaimonCide Apr 18 '19 at 17:41
  • 1
    ToUpper set all characters in uppercase, so we compare your ```var```(boyName) and your string(Bobby) both in uppercase... the result is ```"BOBBY" == "BOBBY" ``` – Paulo Campez Apr 18 '19 at 17:43
  • 1
    Also. .ToLower(); is another method that could be used. Have a look at the string methods you could be using for various tasks. https://learn.microsoft.com/en-us/dotnet/api/system.string?view=netframework-4.7.2 –  Apr 18 '19 at 17:58