如何获取与List()结果匹配的字符串的一部分?

时间:2018-03-25 13:22:04

标签: string vb.net list

所以,这是代码。我想要做的是获取str变量,检查它是否包含strList的条目以及它是否确实返回该条目(因此我可以在IF语句中使用它等等)。哦,是的,我需要在List()中使用变量名称。那么云我是怎么做到的?

Dim strList As New List(Of String)
Dim str As String = "SomeFancyStringThere"
strList.Add(somefancystringname, "Fancy")

我尝试了什么:

Dim strList As New List(Of String)
Dim str As String = "SomeFancyStringThere"
strList.Add(somefancystringname, "Fancy")
If str.Contains(strList.Any) Then
  Dim strFound As String = strList. 'and I am completely stuck there.
End If

提前致谢, DDev。

1 个答案:

答案 0 :(得分:0)

我对你的想法的理解是你试图过滤一个字符串列表。或者用简单的话说,你试图找出你的List(Of String是否包含一个特定的字符串,对吧?嗯,这是最简单的解决方案:

Private MylistOfString As New List(Of String)
 Private Function CheckIfExists(byval myStr as string) As String
 For each item As String in MylistOfString
   If item = myStr Then
     Return myStr
   End if
 Next
 Return ""
End Function

希望这会有所帮助:)