Is there any way to make a case condition in a switch statement where you say if a string begins with something?
ex
Switch (mystring)
{
case("abc")://String begins with abc (abcd or abc1 or abcz or abc.. or abc will fall in this condition).
//Do Something
break;
default:
break;
}
UPDATE Other strings can be different length.
abc..
abczyv
dcs2.
qwerty
as...k
If you knew that the length of conditions you would care about would all be the same length then you could:
switch(mystring.substring(0, Math.Min(3, mystring.Length))
{
case "abc":
//do something
break;
case "xyz":
//do something else
break;
default:
//do a different thing
break;
}
The Math.Min(3, mystring.Length)
is there so that a string of less than 3 characters won't throw an exception on the sub-string operation.
There are extensions of this technique to match e.g. a bunch of 2-char strings and a bunch of 3-char strings, where some 2-char comparisons matching are then followed by 3-char comparisons. Unless you've a very large number of such strings though, it quickly becomes less efficient than simple if-else chaining for both the running code and the person who has to maintain it.
Edit: Added since you've now stated they will be of different lengths. You could do the pattern I mentioned of checking the first X chars and then the next Y chars and so on, but unless there's a pattern where most of the strings are the same length this will be both inefficient and horrible to maintain (a classic case of premature pessimisation).
The command pattern is mentioned in another answer, so I won't give details of that, as is that where you map string patterns to IDs, but they are option.
I would not change from if-else chains to command or mapping patterns to gain the efficiency switch sometimes has over if-else, as you lose more in the comparisons for the command or obtaining the ID pattern. I would though do so if it made code clearer.
A chain of if-else's can work pretty well, either with string comparisons or with regular expressions (the latter if you have comparisons more complicated than the prefix-matches so far, which would probably be simpler and faster, I'm mentioning reg-ex's just because they do sometimes work well with more general cases of this sort of pattern).
If you go for if-elses, try to consider which cases are going to happen most often, and make those tests happen before those for less-common cases (though of course if "starts with abcd" is a case to look for it would have to be checked before "starts with abc").