C#的switch-case字符串开始 [英] C# Switch-case string starting with
问题描述
有没有什么办法,使在switch语句中的case条件,你说,如果一个字符串开头的东西吗?
Is there any way to make a case condition in a switch statement where you say if a string begins with something?
前
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
作为... ... K
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;
}
的
Math.Min(3 ,mystring.Length)
是否有这样的不足3个字符的字符串不会扔在子字符串操作的异常。
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.
有此技术的延伸,以匹配如一堆双字符字符串和一堆3字符的字符串,其中一些双字符比较匹配,然后其次是3个字元比较。除非你非常多的这样的字符串不过,它很快变得比简单的if-else链为运行代码,谁能够维护它的人。
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.
编辑:增加了,因为你现在已经表示,他们将不同的长度。你可以做我所提到的检查第一个X字符,然后下一Ÿ字符等的格局,但除非有一个模式,大部分的字符串的长度相同,这将是一种效率低下和可怕的维护(经典案例过早pessimisation)。
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).
Command模式在另一个答复中提到,所以我不会放弃的那个细节,就是在那里你映射字符串模式为ID,但他们的选择。
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.
我不会改变从的if-else链命令或映射模式,以获得高效开关的有时有超过若-else,因为你失去更多的比较为命令或取得ID模式。我想,虽然这样做,如果它使代码更清晰。
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.
中的if-else的可以工作得很好,无论是与字符串比较或使用正则表达式A链(后者,如果你有比较比前缀匹配,从而更为复杂,这可能会更简单,更快,我提到REG-EX的仅仅是因为他们有时会用这种模式的更一般的情况下)工作。
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).
如果你去,如果-别人的,尽量考虑哪些案件要经常发生,让那些不常见的情况下,之前的那些测试发生(当然,如果开头ABCD是看它都必须检查前案与美国广播公司)开始。
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").
这篇关于C#的switch-case字符串开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!