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 )

4
  • You'll need to loop all elements in x then do contains Commented Jul 4, 2019 at 16:57
  • Can you explain sir? Commented Jul 4, 2019 at 17:00
  • This code is not working 😓 Commented Jul 4, 2019 at 17:01
  • Possible duplicate of Check if a string contains an element from a list (of strings) Commented Jul 5, 2019 at 14:19

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

For x as interger = 0 to 2000 List1.add(x) Next Xe.any......... its working but get more time 😓
A Dictionary I said, not a List!
Please example sir
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

Comments

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

6 Comments

I try this loop but its get more time to find value
Iam use streem reader (string file size - 5.mb )and list of string 2000
@Alex that is information you should include in your question. In your example code "a1" is certainly not 5 mb. How much text is in your text box?
@Alex "ut its get more time to find value" More time than what?
Iam add "a1 " in 20 char and text file more than 3mb . loop is slow what i do now?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.