+ 3

Can I make my code ignore whether or not the answer has capital letters

I'm doing notes to myself in form of little, useless codes everytime I learn new stuff (c#). Now im trying to write this little code that includes switch-statement. string turtle = Console.ReadLine(); switch (turtle) { case "Leonardo": Console.WriteLine("He leads!"); break; //and so on for raph, mike, don and default Thats fine and all, but now my code requires that user writes his answer with a capital letter to make this work. If you just write leonardo without a capital letter, it goes for default. Is there a way to make this work whether or not there is a capital letter or not. Im also wondering, is there a way to make a case work with multiple answers. Like that it would also work if you wrote "Leo". I tried to add it like this: case "Leonardo", "Leo": But it didnt work. Is anything like this possible, or do I need to make another case for "Leo" Thanks for the help beforehand, and sorry for my messy and possible unreadable message :D

1st Jun 2018, 11:56 PM
Kuutti
Kuutti - avatar
3 odpowiedzi
+ 7
In a case like this you need to turn both strings to lower or uppercase letters Or you could only lowercase the first letter string turtle = Console.ReadLine(); switch (turtle.ToLower()) Make sure your cases are also lowercased Multiple cases should work like that. You can also do case "leonardo": case "leo": Console.WriteLine("He leads!");
2nd Jun 2018, 12:01 AM
Toni Isotalo
Toni Isotalo - avatar
+ 2
Thanks man, it worked. I didnt get multiple cases to work my original way (im probably just missing something) but your way worked like a dream.
2nd Jun 2018, 12:21 AM
Kuutti
Kuutti - avatar
0
Yes you can use a delimiter which can ignore 'Y' or 'y' input.
2nd Jun 2018, 4:09 PM
Apple Blossom
Apple Blossom - avatar