switch statment with characters
Hi, I'm trying to write some lines of code using a switch statment to see if certain characters are in a string. Is it possible to do this?
I tried this:
Code:
switch (directoryPath.Contains)
{
case ":":
//some code
break;
case "\\":
//some code
break;
case "?":
//some code
break;
default:
//some code
break;
but obviously that doesn't work. So is there a way to accomplish this via switch statment?
Thanks.
Re: switch statment with characters
That doesn't make any sense. directoryPath.Contains returns a boolean, so the only two cases you can have are true and false.
Switch statements are meant to be fast/constant lookup speed conditionals. If each 'case' needs to be evaluated at runtime you cannot create a jump table when the code is compiled. Just us if/elseif statements; that's what they are there for.
Re: switch statment with characters
You could do it like this:
Code:
for(int i = 0; i < directoryPath.Length; i++)
{
switch(directoryPath[i])
{
case '?': //do stuff
break;
}
}
Re: switch statment with characters
Quote:
Originally Posted by
BigEd781
That doesn't make any sense. directoryPath.Contains returns a boolean, so the only two cases you can have are true and false.
I know it doesn't make any sense... that's why asked is it possible to do something like it using a switch statment. Nested if's can get so messy.
Thanks for the replies all.