- Home /
Syntax of switch + case insensitive?
So there's an input, and some commands, I want to check what people are typing in, or if it's just normal chat message. err, and I use a switch, but I also want a case insensitive on the checks. Example script, from current version of script, this works, but the input has to be case sensitive, or it won't register them..
switch(Console Input From Player)
{
case "/example1" :
{
}
break;
case "/example2" :
{
}
break;
default :
{
}
break;
just FORCE the inco$$anonymous$$g information to be all lower case. then, compare.
makes sense?! hope it helps !
It does help, but that also means, that if it doesn't match any of the given parameters, then outputs as chat, if you say "Hi there" it'll make you look like a spelling retard and put "hi there" Although the method is valid.
Dude - you make ANOTHER VARIABLE that has the lower case version. Use that OTHER variable to match. $$anonymous$$eep the original intact.
O$$anonymous$$? That's how you do it.
This is really basic program$$anonymous$$g questions - you should use stackoverflow. this is really more for Unity3D specific answers, I think.
certain things are not easy to figure out for beginners, esp if you are self tought like me you get weird gaps in your knowledge which might seem very basic to others but you simply didnt know about it
Answer by FlowerCraft · Jul 24, 2012 at 10:29 PM
This was resolved by myself, before Pheash answer, posting answer for future reference to the community:
The problem was, that I couldn't assign the comparison that was case in-dependent, so what I did, was instead of adding them on a case basis, just lower casing the entire search option as follows:
switch([STRING].ToLower () ){
Answer by pheash · Jul 24, 2012 at 08:45 AM
Hi, This might be more of a pointer into a possible direction, as i havent tried it or done it myself: you can use .NET functions have a look here: http://docs.unity3d.com/Documentation/ScriptReference/MonoCompatibility.html there is one in there called ToLower() which converts a string to lower case.
1) convert the user input to lower case
2) compare lowercase version to your list via switch statement
That way it doesnt matter if the user input is "\\ExAmPLe1" or "\\exaMple1"
I know that normal java has a function called: compareToIgnoreCase(String), it compares 2 strings and ignores the case. I am not 100% sure you can use that one
Yes, js does have that, and I'm trying to use that, but ins$$anonymous$$d of an if, then with a switch&case statement, just trying to figure the syntax of it.
I dont see anything wrong with your syntax above, aslong as "Console Input..." is replaced by a string variable holding the lowerCase version of the input. although I have read that a string stored in a variable will never be the same as another string that looks exactly the same but isnt the same object. maybe someone can confirm or clarify this point. that might be why your switch doesnt work.
Your answer
![](https://koobas.hobune.stream/wayback/20220613074614im_/https://answers.unity.com/themes/thub/images/avi.jpg)