我有54,000个文件,每个文件的命名方法略有不同,我需要检查文件名是否包含特定的字符串。但是,我无法找到使Select语句正常工作的方法。
我的代码意外失败。请任何人都可以告诉我为什么字符串" BAR"在" BARITONE"?
中找不到Dim tempCheck as string = "1st BARITONE"
Dim GoodOne as Boolean = False
Select Case tempCheck
Case tempCheck.Contains("CORN")
GoodOne = True
Case tempCheck.Contains("HORN")
GoodOne = True
Case tempCheck.Contains("BAR")
GoodOne = True
Case tempCheck.Contains("TROM")
GoodOne = True
Case tempCheck.Contains("EUP")
GoodOne = True
Case Else
GoodOne = False
End Select
答案 0 :(得分:4)
试试这样:
Dim tempCheck as string = "1st BARITONE"
Dim GoodOne as Boolean = False
Select Case True
Case tempCheck.Contains("CORN")
GoodOne = True
Case tempCheck.Contains("HORN")
GoodOne = True
Case tempCheck.Contains("BAR")
GoodOne = True
Case tempCheck.Contains("TROM")
GoodOne = True
Case tempCheck.Contains("EUP")
GoodOne = True
Case Else
GoodOne = False
End Select
SELECT CASE对这样的字符串不起作用(你必须将字符串与字符串进行比较,在这种情况下你要比较一个布尔值)。与布尔相比,它确实适用于布尔值
答案 1 :(得分:3)
您使用的是Select Case错误。只需使用简单的if else:
Dim tempCheck As String = "1st BARITONE"
Dim GoodOne As Boolean = False
If tempCheck.Contains("CORN") Then
GoodOne = True
ElseIf tempCheck.Contains("HORN") Then
GoodOne = True
ElseIf tempCheck.Contains("BAR") Then
GoodOne = True
ElseIf tempCheck.Contains("TROM") Then
GoodOne = True
ElseIf tempCheck.Contains("EUP") Then
GoodOne = True
End If