Search for matches in a text string from a list?

0

I want to develop a very simple application but I'm blocked because I do not know how to start.

The idea is:

  • I have a list of authors in a TXT file

  • I introduce a text string where several authors are separated by commas in a text box

  • Then, if any author of that string matches those in the list, it will appear in another separate text box

Conceptual example:

list=" Lastname1 Firstname1 , LastName2 Firstname2, LastName3, Firstname3 "

string = Surname5 Name5, Lastname1 Firstname1 , Lastname3 Firstname3 "

Result="Surname1 Name1, Surname3 Name3"

Preview:

Imports System
Imports System.IO

Public Class Form1

    Public Shared file As StreamReader = New StreamReader("C:\Users\User\Desktop\autores.txt")
    Public Shared autores As String = file.ReadToEnd

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    TextBox2.Text = autores

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

End Sub
End Class
    
asked by sydragon 20.01.2018 в 16:38
source

1 answer

1

Based on my comment, I hope that in your list "Surname3, Name3" should not have the separation comma.

You could consider:

Public Class Form1
    Private Shared _autores As String

    Private Shared Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim sr = New StreamReader("D:\autores.txt")
        _autores = sr.ReadToEnd()
        sr.Close()

    End Sub

    Private Sub BtnBuscarCoincidencias_Click(sender As Object, e As EventArgs) Handles btnBuscarCoincidencias.Click
        ' Convertir cadena de autores a una lista, donde la separación es ", "
        Dim listaAutores = _autores.Split(New String() {", "}, StringSplitOptions.RemoveEmptyEntries)

        ' Obtener nueva cadena de autores
        Dim cadena = txtAutores.Text

        ' Convertir cadena a una nueva lista, donde la separación es ", "
        Dim nuevaListaAutores = cadena.Split(New String() {", "}, StringSplitOptions.RemoveEmptyEntries)

        ' Cadena resultado
        Dim resultado = ""

        ' Recorrer cada lista para buscar los que son iguales
        For Each autor As String In listaAutores
            For Each nuevoAutor As String In nuevaListaAutores

                If autor = nuevoAutor Then

                    resultado = resultado & autor & ", "

                End If

            Next
        Next

        txtResultado.Text = resultado

    End Sub

End Class

Where,

  • .Split(New String() {", "}, StringSplitOptions.RemoveEmptyEntries) : Returns a list of strings, where the separation string is the comma and empty (","). Also, StringSplitOptions.RemoveEmptyEntries does not consider empty elements.

If you do not want to consider the For Each , you could use Intersect . For example:

Dim interseccion As IEnumerable(Of String) = listaAutores.Intersect(nuevaListaAutores)
txtResultado.Text = String.Join(", ", interseccion)

Reference:

answered by 20.01.2018 / 22:46
source