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



:)

我有这种情况:我有一个由我定义的对象列表,除了一些其他数据字段外,每个人都有一个字符串字段。对于每个对象,我必须验证此字符串字段是否包含一系列子字符串组合、不包含或部分包含它们。所以我做了一个很长的 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"上。但无论如何,有许多不同复杂性的条件,我认为默认的解析器函数将不起作用,也因为我正在尝试在字符串中转换一系列关于字符串比较的布尔条件。

希望有一些提示和建议。

正如@Plutonix所说,使用 .包含((, .IndexOf(( 和 .Substring(( 方法将使你的代码更加干净。另外,不确定您在最后一段中要做什么,但如果您想在字符串中加入双引号,则必须使用 2。

例:

Dim condition As String = "((InStr(1, art.description, ""string1"") > 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

最新更新