string VB.NET - 如果字符串包含“value1”或“value2”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6370081/
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 01:09:00  来源:igfitidea点击:

VB.NET - If string contains "value1" or "value2"

vb.netstringif-statementcontains

提问by Kenny Bones

I'm wondering how I can check if a string contains either "value1" or "value2"? I tried this:

我想知道如何检查字符串是否包含“value1”或“value2”?我试过这个:

If strMyString.Contains("Something") Then

End if

This works, but this doesn't:

这有效,但这不起作用:

If strMyString.Contains("Something") or ("Something2") Then

End if

This gives me the error that conversion from string to Long can't be done. If I put the or ("Something2")inside the parenthesis of the first one, it gives me the error that the string cannot be converted to Boolean.

这给了我无法完成从字符串到 Long 的转换的错误。如果我把or ("Something2")第一个的括号里面,它给了我字符串无法转换为布尔值的错误。

So how can I check if the string contains either "string1" or "string2" without having to write too much code?

那么如何在不必编写太多代码的情况下检查字符串是否包含“string1”或“string2”?

回答by Rifky

You have to do it like this:

你必须这样做:

If strMyString.Contains("Something") OrElse strMyString.Contains("Something2") Then
    '[Put Code Here]
End if

回答by Ash Burlaczenko

You need this

你需要这个

If strMyString.Contains("Something") or strMyString.Contains("Something2") Then
    'Code
End if

回答by Matt Wilko

In addition to the answers already given it will be quicker if you use OrElseinstead of Orbecause the second test is short circuited. This is especially true if you know that one string is more likely than the other in which case place this first:

除了已经给出的答案之外,如果您使用OrElse而不是Or因为第二个测试短路,它会更快。如果您知道一个字符串比另一个字符串更有可能,则尤其如此,在这种情况下将其放在首位:

If strMyString.Contains("Most Likely To Find") OrElse strMyString.Contains("Less Likely to Find") Then
    'Code
End if

回答by Predator

Here is the alternative solution to check whether a particular string contains some predefined string. It uses IndexOfFunction:

这是检查特定字符串是否包含某些预定义字符串的替代解决方案。它使用IndexOf功能:

'this is your string
Dim strMyString As String = "aaSomethingbb"

'if your string contains these strings
Dim TargetString1 As String = "Something"
Dim TargetString2 As String = "Something2"

If strMyString.IndexOf(TargetString1) <> -1 Or strMyString.IndexOf(TargetString2) <> -1 Then

End If

NOTE:This solution has been tested with Visual Studio 2010.

注意:此解决方案已经过 Visual Studio 2010 测试。

回答by Oded

You have ("Something2")by itself - you need to test it so a boolean is returned:

("Something2")自己拥有 - 您需要对其进行测试,以便返回一个布尔值:

If strMyString.Contains("Something") or strMyString.Contains("Something2") Then

回答by agent-j

If strMyString.Contains("Something") or strMyString.Contains("Something2") Then

End if

The error indicates that the compiler thinks you want to do a bitwise ORon a Boolean and a string. Which of course won't work.

该错误表明编译器认为您想对OR布尔值和字符串进行按位运算。这当然行不通。

回答by Android

 If strMyString.Tostring.Contains("Something") or strMyString.Tostring.Contains("Something2") Then


     End if

回答by Dunderchief

Interestingly, this solution can break, but a workaround: Looking for my database called KeyWorks.accdb which must exist:

有趣的是,此解决方案可能会中断,但有一个解决方法:查找必须存在的名为 KeyWorks.accdb 的数据库:

Run this:

运行这个:

Dim strDataPath As String = GetSetting("KeyWorks", "dataPath", "01", "") 'get from registry

Dim strDataPath As String = GetSetting("KeyWorks", "dataPath", "01", "") '从注册表中获取

If Not strDataPath.Contains("KeyWorks.accdb") Then....etc.

如果不是 strDataPath.Contains("KeyWorks.accdb") 那么......等。

If my database is named KeyWorksBB.accdb, the If statement will find this acceptable and exit the if statement because it did indeed find KeyWorks and accdb.

如果我的数据库名为 KeyWorksBB.accdb,则 If 语句会认为这可以接受并退出 if 语句,因为它确实找到了 KeyWorks 和 accdb。

If I surround the If statement qualifier with single quotes like ("'KeyWorks.accdb'"), it now looks for all the consecutive characters in order and would enter the If block because it did not match.

如果我用像 ("'KeyWorks.accdb'") 这样的单引号将 If 语句限定符括起来,它现在会按顺序查找所有连续字符,并会进入 If 块,因为它不匹配。