0

How to find list of string in textbox using vb.net

I try this code

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

How to find big list in big string lees time (true or false )

Alex
  • 1
  • 4

4 Answers4

1

You can use the Any function to look at each value of x and see if it is contained in the text:

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
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
0

The Contains function cannot take a list as an argument. It must have a string, in this case the individual items of the List(Of String). You can loop through the items of the list with a For Each loop.

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

EDIT I am not sure if this will speed things up but give it a try.

    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
Mary
  • 14,926
  • 3
  • 18
  • 27
0

Put your list into a dictionary (you can specify whether the comparison should be case sensitive or not by providing a StringComparer.InvariantCulture or StringComparer.InvariantCultureIgnoreCase), 5MB is no problem.

Split your text into lines (or whatever delimiter you are using) and look them up in the dictionary.

Christoph
  • 3,322
  • 2
  • 19
  • 28
0

Assuming your lookup table is rather static (may be reloaded occasionally but not for every comparison), you can try something like this. I also assumed you like the strings to be compared case insensitively (otherwise remove the lines that call .ToLowerInvariant()).

StartUp.vb: (Provides a property with the lookup table and a method to reload it.)

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: (Provides an event handler for an OK-button as well as a function to lookup which lines match the strings in the lookup table)

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
Christoph
  • 3,322
  • 2
  • 19
  • 28