vb.net 你能在 Select Case 语句中使用 .Contains(string) 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2647771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:49:40  来源:igfitidea点击:

Can you use .Contains(string) with a Select Case Statement?

vb.netselect-case

提问by Lou

Is there anyway I can build a Select statement that uses the Contains function? Like this:

无论如何,我可以构建一个使用 Contains 函数的 Select 语句吗?像这样:

Select commentStr
    Case commentStr.Contains("10")
    Case commentStr.Contains("15")

回答by AakashM

Select Case True
    Case commentStr.Contains("10")
        'foo
    Case commentStr.Contains("15")
        'bar
End Select

Note that with this construct, a maximum of oneCasewill be executed.

请注意,这个结构,最大的一个Case将被执行。

(Also note that your C# friends can'tdo this with switch, which requires constant expressions in the caseclauses :))

(另请注意,您的 C# 朋友不能使用switch,这需要在case子句中使用常量表达式:))