Help: checking a variable against a string ignoring case senstivity.
-
This is giving me a real headache, I hope someone out there can help me. :-) Basically what I have is a text based adventure that takes input from the user. for example... string input; Console.WriteLine("Where do you want to go?"); input = Console.ReadLine(); if ((input = "north") || (input = "North") || (input = "n") || (input = "N") { if ((y - 1) < 0) Console.WriteLine(You can't move any farther North"); else y = y - 1; } the bit that I want to change is where I check the input vs 4 values, north, North, n, and N. Is there any shorter way of doing this, say checking it for letters and ignoring case sensitivity. I suppose I could trim off the first letter, but then people could put anything in then like, nowhere, and they end up going north. Thanks for your help ahead of time.
-
This is giving me a real headache, I hope someone out there can help me. :-) Basically what I have is a text based adventure that takes input from the user. for example... string input; Console.WriteLine("Where do you want to go?"); input = Console.ReadLine(); if ((input = "north") || (input = "North") || (input = "n") || (input = "N") { if ((y - 1) < 0) Console.WriteLine(You can't move any farther North"); else y = y - 1; } the bit that I want to change is where I check the input vs 4 values, north, North, n, and N. Is there any shorter way of doing this, say checking it for letters and ignoring case sensitivity. I suppose I could trim off the first letter, but then people could put anything in then like, nowhere, and they end up going north. Thanks for your help ahead of time.
hi, you could insert input=input.ToLower(); and forget about uppercase. :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
hi, you could insert input=input.ToLower(); and forget about uppercase. :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
This is giving me a real headache, I hope someone out there can help me. :-) Basically what I have is a text based adventure that takes input from the user. for example... string input; Console.WriteLine("Where do you want to go?"); input = Console.ReadLine(); if ((input = "north") || (input = "North") || (input = "n") || (input = "N") { if ((y - 1) < 0) Console.WriteLine(You can't move any farther North"); else y = y - 1; } the bit that I want to change is where I check the input vs 4 values, north, North, n, and N. Is there any shorter way of doing this, say checking it for letters and ignoring case sensitivity. I suppose I could trim off the first letter, but then people could put anything in then like, nowhere, and they end up going north. Thanks for your help ahead of time.
you can turn the whole position of the string to upper or lower case: String str = "aBcD"; str = str.ToUpper(); if(str == "ABCD") { // Bla Bla Bla } when the IP reaches the IF check, it alwayse will enter to // Bla Bla Bla:~
-
Another option is String.Compare(s1, s2, true), where the true indicates to ignore case.