B4J Code Snippet Search in a List

Hello, here I come to you with a simple way to search for a word in a list of words. It is a very simple but very useful function.

It is declared as ResumableSub so you can use Wait For and wait for it to finish searching to get the results.


Example:

Example::
Dim Fruits as List = Array as String("Apple","Blueberry","Lime","Orange","Pear","Pineapple","Lemon","Bananna","Peach")

Wait For(searchInList("P", Fruits)) Complete(result As List) 'Searching for "P" matches
'Result = Array("Pineapple", "Peach")

Wait For(searchInList("B", Fruits)) Complete(result As List) 'Searching for "B" matches
'Result = Array("Blueberry", "Bananna")


Dim Names as List = Array as String("Alejandro", "Ana", "Antonio", "Alicia", "Andres", "Beatriz", "Bruno", "Blanca", "Benjamin", "Barbara", "Carlos", "Carmen", "Claudia", "Cristina", "Cesar", "Diego", "Daniela", "David", "Diana", "Dolores", "Elena", "Eduardo", "Emilia", "Esteban", "Elisa", "Fernando", "Felipe", "Florencia", "Francisco", "Fatima", "Gabriel", "Gloria", "Guillermo", "Gustavo", "Gabriela", "Hector", "Helena", "Hugo", "Hilda", "Horacio")

Wait For(searchInList("An", Names)) Complete(result As List) 'Searching for "An" matches
'Result = Array("Ana", "Antonio", "Andres")


B4X:
Public Sub searchInList(Text As String, Lst As List) As ResumableSub
    Dim temp_result As List : temp_result.initialize
    For Each str As String In Lst
     Dim lowercase as String = str.toLowerCase
        If lowercase.Contains(Text.toLowerCase) Then
            temp_result.Add(str)
        End If
    Next
    Return temp_result
End Sub
 
Last edited:

Mark Read

Well-Known Member
Licensed User
Longtime User
Hello, here I come to you with a simple way to search for a word in a list of words. It is a very simple but very useful function.

It is declared as ResumableSub so you can use Wait For and wait for it to finish searching to get the results.


Example:

Example::
Dim Fruits as List = Array as String("Apple","Blueberry","Lime","Orange","Pear","Pineapple","Lemon","Bananna","Peach")

Wait For(searchInList("P", Fruits)) Complete(result As List) 'Searching for "P" matches
'Result = Array("Pineapple", "Peach")

Wait For(searchInList("B", Fruits)) Complete(result As List) 'Searching for "B" matches
'Result = Array("Blueberry", "Bananna")


Dim Names as List = Array as String("Alejandro", "Ana", "Antonio", "Alicia", "Andres", "Beatriz", "Bruno", "Blanca", "Benjamin", "Barbara", "Carlos", "Carmen", "Claudia", "Cristina", "Cesar", "Diego", "Daniela", "David", "Diana", "Dolores", "Elena", "Eduardo", "Emilia", "Esteban", "Elisa", "Fernando", "Felipe", "Florencia", "Francisco", "Fatima", "Gabriel", "Gloria", "Guillermo", "Gustavo", "Gabriela", "Hector", "Helena", "Hugo", "Hilda", "Horacio")

Wait For(searchInList("An", Names)) Complete(result As List) 'Searching for "An" matches
'Result = Array("Ana", "Antonio", "Andres")


B4X:
Public Sub searchInList(Text As String, Lst As List) As ResumableSub
    Dim temp_result As List : temp_result.initialize
    For Each str As String In Lst
        If str.Contains(Text) Then
            temp_result.Add(str)
        End If
    Next
    Return temp_result
End Sub
I like the post, short and to the point, thanks. However the match is case sensitive which means in the third example "Blanca" will not be found. One of the problems with people today is not checking when they enter data which could mean that a name starts in lower case.

Regards
 

Brian Michael

Active Member
Licensed User
You guys are right, i set all lowercase letters.

Thank you for your contribution to the snippet!
 

Alessandro71

Well-Known Member
Licensed User
Longtime User
It does not look like a resumable sub to me: no Sleep or other way to switch to process the event queue
 

Brian Michael

Active Member
Licensed User
I believe for a long list its useful to be a ResumableSub, i had a list with +100,000 rows and get a optimal result.
 
Top