Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何检查是否在字符串列表中找到子字符串_Vb.net_Dictionary - Fatal编程技术网

Vb.net 如何检查是否在字符串列表中找到子字符串

Vb.net 如何检查是否在字符串列表中找到子字符串,vb.net,dictionary,Vb.net,Dictionary,我有一本字典,其键值(字符串类型)如下: 标记 标记a 马克b 橙色的 苹果 苹果a 我还有一个列表(字符串),其中包含: 标记 苹果 我想删除字典中的任何条目,其中列表中的任何值都是字典项键的子字符串 在上面的示例中,具有以下键的条目将从字典中删除: 标记 标记a 马克b 苹果 苹果a 如何在没有多个循环的情况下从字典中筛选和删除这些条目 以下是我尝试过的示例代码/伪代码: For Each whatKVP In whatDict If IsSubstringFoundIn

我有一本字典,其键值(字符串类型)如下:

  • 标记
  • 标记a
  • 马克b
  • 橙色的
  • 苹果
  • 苹果a
我还有一个列表(字符串),其中包含:

  • 标记
  • 苹果
我想删除字典中的任何条目,其中列表中的任何值都是字典项键的子字符串

在上面的示例中,具有以下键的条目将从字典中删除:

  • 标记
  • 标记a
  • 马克b
  • 苹果
  • 苹果a
如何在没有多个循环的情况下从字典中筛选和删除这些条目

以下是我尝试过的示例代码/伪代码:

For Each whatKVP In whatDict
    If IsSubstringFoundInAList(whatKVP.Key, listName) = True Then
        listKeyDel.Add(whatKVP.Key)
    End If
Next

For Each whatKey In listKeyDel
    whatDict.Remove(whatKey)
Next

Private Function IsSubstringFoundInAList(ByVal strIp As String, ByVal listStr As List(Of String)) As Boolean 
    Dim whatStr As String

    For Each whatStr In listStr
        If strIp.Contains(whatStr) = True Then
            Return True
        End If
    Next

    Return False
End Function
在上面的代码中,我需要使用许多(3)个循环来完成任务。是否有更好的方法/更简洁的代码来完成它?

您可以使用List.any()在我看来更简洁

Dim findString = "foo"
listOfString.Any(Function(stringInList)
        Return stringInList.Equals(findString)
    End Function)

不过,如果您希望提高性能,我建议您使用

您可以在一句话中完成这一点。下面假设
whatDict
是您的字典,
listName
是您的关键子字符串列表

whatDict = whatDict.Where(Function(kvp) Not listName.Any(Function(n) kvp.Key.Contains(n))) _
    .ToDictionary(Function(kvp) kvp.Key, Function(kvp) kvp.Value)

这将创建一个新字典,其中仅包含现有字典中的KeyValuePairs,而该字典中的键不包含
listName

中的任何子字符串。此函数应实现以下功能:

Public Function FilterDictionary(Of T, K)(Dictionary As IDictionary(Of T, K), FilterList As ICollection(Of T)) As IDictionary(Of T, k)
    Return (From Item In Dictionary
            Where Not FilterList.Any(Function(x) Item.Key.ToString.Contains(x.ToString))).
            ToDictionary(Function(x) x.Key, Function(x) x.Value)
End Function
用法:

Dim MyDictionary As New Dictionary(Of String, Integer)
Dim FilterList As New List(Of String)
'Fill Collections...
Dim FilteredDictionary = FilterDictionary(MyDictionary, FilterList)

你需要这样的东西。我使用了“StartsWith”,但您可以更改为“Contains”

    Dim outList As New List(Of String)
    outList.Add("mark")
    outList.Add("apple")

    whatDict = whatDict.Where(Function(x) Not outList.Exists(Function(y) x.Key.StartsWith(y))).ToDictionary(Function(x) x.Key, Function(x) x.Value)

你有没有试过的代码?如果你不提供任何内容,版主会投票否决你。@KeithAymar,我找到的最接近的帖子在这里,但我不知道如何检查字符串列表中是否找到字符串的子字符串。在你的示例中,它似乎不是子字符串,而是以开头。