1

我有 54,000 个文件,每个文件的命名方法略有不同,我需要检查文件名是否包含特定的字符串。但是,我找不到让 Select 语句正常工作的方法。

我的代码意外失败。请谁能告诉我为什么在“BARITONE”中找不到字符串“BAR”?


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
4

2 回答 2

6

试试这样:

    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不适用于这样的字符串(您必须将字符串与字符串进行比较,在这种情况下您将与布尔值进行比较)。与布尔值相比,它确实适用于布尔值

于 2013-07-31T13:43:04.140 回答
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
于 2013-07-31T13:43:57.690 回答