控制一系列字符串是否在VB.NET中包含一系列子字符串组合

时间:2017-07-13 22:53:10

标签: string vb.net parsing string-comparison

:)

我遇到这种情况:我有一个由我定义的对象列表,除了一些其他数据字段外,每个人都有一个字符串字段。我必须为每个对象验证此字符串字段是否包含一系列子字符串组合,不包含或部分包含它们。所以我已经制作了一个非常长的If指令列表(不是Select Case,因为子字符串的组合不排除任何其他),现在我认为代码看起来非常糟糕,有点像:

For Each art As Article In ArticlesList

If (((InStr(1, art.description, "string1") > 0) And (InStr(1, art.description, "string2") > 0)) Or ((InStr(1, art.description, "string3") > 0) And (InStr(1, art.description, "string4") > 0) And (InStr(1, art.description, "string8") > 0))) Then

'...do something

End if


If (((InStr(1, art.description, "string4") > 0)) Or ((InStr(1, art.description, "string3") = 0) And (InStr(1, art.description, "string5") = 0) Or (InStr(1, art.description, "string9") > 0))) Then

'...do something

End if
'...
'...
'...
'... many other If (about 80 or more)

Next

所以,总的来说,我想问一种方法来制作更好看和更有效的代码。特别是我想限制Instr()的出现,我认为如果我将这些布尔条件放在某个数组或List中并在需要时用布尔表达式解析它们,滚动字符串列表和具有布尔条件的字符串会更好在一起,但是例如我不能把条件放入

Dim condition as String = "((InStr(1, art.description, "string1") > 0) And (InStr(1, art.description, "string2") > 0)) Or ((InStr(1, art.description, "string3") > 0) And (InStr(1, art.description, "string4") > 0) And (InStr(1, art.description, "string7") > 0))"

因为Visual Studio在“string1”上说“预期指令结束”。但无论如何,有许多复杂的条件,我认为默认的解析器函数不起作用,也导致我试图在字符串中转换一系列与字符串比较有关的布尔条件。

希望提供一些提示和建议。

2 个答案:

答案 0 :(得分:2)

正如@Plutonix所说,使用.Contains(),. IndexOf()和.Substring()方法将使您的代码更加清晰。此外,不确定你在最后一段中尝试做什么,但如果你想在字符串中加双引号,你必须使用2。

示例:

Dim condition As String = "((InStr(1, art.description, ""string1"") > 0)"

答案 1 :(得分:0)

一些事情。首先,您可以将要尝试查找的字符串放在数组中,然后使用Any

If {"string1", "string2", "string3", "string4", "string8"}.Any(function(x) return article.description.contains(x)) Then
    ... do something
End if

我们还可以创建一个表达式以传递给Any,并可以使用ForEach来枚举列表:

    Dim ArticlesList As New List(Of Article)
    ArticlesList.ForEach(
        Sub(article)
            Dim Matcher = Function(x)
                              Return article.description.Contains(x)
                          End Function
            If {"string1", "string2", "string3", "string4", "string8"}.Any(Matcher) Then
                'Do something
            End If
            If {"string4", "string3", "string9"}.Any(Matcher) Then
                'Do something
            End If
        End Sub)

您可以获得创意,并创建一个字符串数组列表,您希望在找到匹配项时调用的代理列表中进行搜索:

Public Class CallBack
    Public IsMatch As Func(Of String, Boolean)
    Public CallBack = Sub() End
End Class
Public Shared Sub DoSomething()
    Console.WriteLine("did something")
End Sub
Public Shared Sub DoSomethingElse()
    Console.WriteLine("did something else")
End Sub
Public Shared Sub DoEvenSomethingElse()
    Console.WriteLine("did even something else")
End Sub
Public Sub FindMatches2()

    Dim CallBacks As New List(Of CallBack)(
    {
         New CallBack With
         {
            .IsMatch = Function(a) (a.Contains("string1") Or a.Contains("string2")) And a.Contains("string9"),
            .Routine = Sub() DoSomething()
    },
         New CallBack With
         {
            .IsMatch = Function(a) a.Contains("string1") Or (a.Contains("string2") And a.Contains("string8")),
            .Routine = Sub() DoSomething()
         }
    }
    )

    Dim ArticleList As New List(Of Article)
    ArticleList.Add(New Article() With {.description = "string1"})


    ArticleList.ForEach(
        Sub(article)
            CallBacks.ForEach(
            Sub(matcher)
                If matcher.IsMatch(article.description) Then
                    Dim result = matcher.Routine
                End If
            End Sub
            )
        End Sub)
End Sub