是否可以使用内置函数将字符串与数组中的所有值进行比较?像这样:
Dim NormalString as string = "Test"
Dim ArrayString = New String() {"Test", "Test2", "Test3"}
If NormalString = ArrayString.AllItems Then
Debug.WriteLine("String found in Array")
End If
或者我必须在循环中迭代字符串,并在循环中逐一比较它们?
注意:我知道ArrayString.AllItems
不是可用的函数。
If ArrayString.Any(Function(s) s = NormalString) Then
您可以在任何IEnumerable(Of T)
上调用Any
,因此可以调用任何数组、集合或LINQ查询。调用不带参数的Any
相当于测试Count
是否大于零。通过一个参数,它将告诉您列表中是否有任何项目满足指定的条件。
对于精确字符串:
Function ExistExactStringInArray(sourceArr() As String, searchedString As String, Optional ignoreCase As Boolean = True) As Boolean
Return (From element As String In sourceArr
Where CBool(If(ignoreCase, (
element?.Trim.ToLower = searchedString?.Trim.ToLower),
(element = searchedString)))).Count > 0
End Function
对于部分字符串:
Function ExistPartialStringInArray(sourceArr() As String, searchedString As String, Optional ignoreCase As Boolean = True) As Boolean
Return (From element As String In sourceArr
Where CBool(If(ignoreCase,
element?.Trim.ToLower.IndexOf(searchedString?.Trim.ToLower) > -1,
element.IndexOf(searchedString) > -1))).Count > 0
End Function