如何使用vb.net查找字符串列表

时间:2019-07-04 16:53:27

标签: vb.net

如何使用vb.net在文本框中查找字符串列表

我尝试这段代码

Dim x as New List(Of String)
X.Add("a1") 
X.Add("a2")
X.Add("a3")
X.Add("a4")
If TextbBox1.Text.Contains(x) Then
    'Code'
End If

如何在大字符串余时间(正确或错误)中查找大列表

4 个答案:

答案 0 :(得分:1)

您可以使用Any函数查看x的每个值,并查看其是否包含在文本中:

Dim sampleText = "x1 y1 z1 a2"
Dim x As New List(Of String) From {"a1", "a2", "a3", "a4"}

If x.Any(Function(y) sampleText.IndexOf(y, StringComparison.InvariantCulture) >= 0) Then
    ' code
End If

答案 1 :(得分:0)

Contains函数不能将列表作为参数。它必须有一个字符串,在这种情况下为List(Of String)的各个项目。您可以使用For Each循环遍历列表项。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim x As New List(Of String)
    x.Add("a1")
    x.Add("a2")
    x.Add("a3")
    x.Add("a4")
    For Each s In x
        If TextBox1.Text.Contains(s) Then
            'Code
        End If
    Next

End Sub

编辑 我不确定这是否可以加快速度,但可以尝试一下。

    Dim x As New List(Of String)
    x.Add("a1")
    x.Add("a2")
    x.Add("a3")
    x.Add("a4")
    Dim LargeString = TextBox1.Text
    For Each s In x
        If LargeString.Contains(s) Then
            'Code
        End If
    Next

答案 2 :(得分:0)

将您的列表放入字典中(您可以通过提供StringComparer.InvariantCulture或StringComparer.InvariantCultureIgnoreCase来指定比较是否区分大小写),5MB没问题。

将您的文本分成几行(或使用的任何定界符),然后在字典中查找它们。

答案 3 :(得分:0)

假设您的查找表是静态的(可能会不时地重新加载,但并非每次比较时都会重新加载),则可以尝试执行类似的操作。我还假设您希望对字符串进行不区分大小写的比较(否则请删除调用.ToLowerInvariant()的行)。

StartUp.vb: (提供具有查找表的属性和重新加载它的方法。)

Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Windows.Forms

Module StartUp

    <STAThread>
    Sub Main(args As String())
        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)
        Application.Run(New MainForm())
    End Sub

    Private _LookupTable As Dictionary(Of String, String)

    Public ReadOnly Property LookupTable As Dictionary(Of String, String)
        Get
            Dim myResult As Dictionary(Of String, String) = _LookupTable
            If (myResult Is Nothing) Then
                myResult = New Dictionary(Of String, String)(StringComparer.Ordinal)
                Const myFilePath As String = "C:\Temp\Foo.txt"
                For Each myLine As String In File.ReadAllLines(myFilePath)
                    'Ignore leading and tailing white-space as well as empty lines
                    myLine = myLine.Trim()
                    If (myLine.Length = 0) Then Continue For
                    'Apply some optimizations
                    Dim myLineLC As String = myLine.Normalize()
                    myLineLC = myLineLC.ToLowerInvariant()
                    myLineLC = myLineLC.Normalize()
                    myLineLC = String.Intern(myLineLC)
                    'Add the line to the dictionary (we like to ignore duplicates, therefore we don't use Add() which would throw an exception is such a case)
                    myResult(myLineLC) = myLine
                Next
                _LookupTable = myResult
            End If
            Return myResult
        End Get
    End Property

    Public Sub ReloadLookupTable()
        _LookupTable = Nothing
    End Sub

End Module

MainForm.vb:(为“确定”按钮提供事件处理程序以及用于查找与查找表中的字符串匹配的行的函数)

Imports System
Imports System.Collections.Generic

Public Class MainForm

    Private Sub btnOkay_Click(sender As Object, e As EventArgs) Handles btnOkay.Click
        Dim myLines As String() = TextBox1.Lines
        For Each myFound As String In LookupLines(myLines)
            'do something with the found strings
        Next
    End Sub

    Private Iterator Function LookupLines(lines As IEnumerable(Of String)) As IEnumerable(Of String)
        If (lines Is Nothing) Then Throw New ArgumentNullException(NameOf(lines))
        Dim myLookupTable As Dictionary(Of String, String) = LookupTable
        For Each myLine As String In lines
            'Ignore leading and tailing white-space as well as empty lines
            myLine = myLine.Trim()
            If (myLine.Length = 0) Then Continue For
            'Apply some optimizations
            myLine = myLine.ToLowerInvariant() 'like this we avoid IgnoreCase comparison
            myLine = myLine.Normalize() 'like this we can use Ordinal comparison instead of InvariantCulture one.
            myLine = String.Intern(myLine) 'like this two same strings return the same reference which is exceptionally fast to compare
            'Check whether the dictionary contains the line
            Dim myResult As String = Nothing
            If (myLookupTable.TryGetValue(myLine, myResult)) Then Yield myResult
        Next
    End Function

End Class