Wish List.Contains

LucaMs

Expert
Licensed User
Longtime User
I'm not asking this for myself, but for a friend: ChatGPT 😁.

It generated something like this:
B4X:
Button1.Enabled = SomeList.Contains("Open a new 'Wish'")
It's a mistake but it makes a lot of sense. It's annoying (and less clear) to have to write:
B4X:
Button1.Enabled = SomeList.IndexOf("Open a new 'Wish'") <> - 1
 
Last edited:
For Example:

📦 Library Name: TextTools
🔹 Step 1: Create a Code Module in B4A
Go to Project > Add New Module > Code Module and name it TextTools.
🔹 Step 2: Add this code inside the module

B4X:
 'TextTools.bas ' A mini library for text-related functions
 Sub Process_Globals   
 ' No global variables needed for now End Sub
 ' Returns True if the keyword is found in the fullText
 Public Sub TranslateToContains(fullText As String, keyword As String) As Boolean 
 Return fullText.ToLowerCase.Contains(keyword.ToLowerCase)
 End Sub

🔍 How to Use It in Your App
B4X:
if TextTools.TranslateToContains("This is just a demo", "demo") Then    
Log("The word is present!")
Else   
 Log("The word is not there.") 
End If
💡 This simple module acts like a mini library and can be exported later as a .jar and .xml if you want to make it shareable across projects.


;)
 
[CODE lang="b4x" highlight="4"]Sub RunTest
Button1.Enabled = SomeList.Contains("LucaMS's Wish")
Log(Button1.Enabled) 'false
Button1.Enabled = SomeList.Contains("Open a new 'Wish'")
Log(Button1.Enabled) 'true
End Sub[/CODE]

[CODE lang="b4x" title="Main"]Sub Process_Globals
Private MainForm As Form
Private Button1 As B4XView
Private SomeList As StringList
End Sub

Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
MainForm.Show
CreateList
RunTest
End Sub

Sub CreateList
SomeList.Initialize
SomeList.Add("Open a new 'Wish'")
End Sub[/CODE]

[CODE lang="b4x" title="StringList Class"]Sub Class_Globals
Private mList As List
End Sub

Public Sub Initialize
mList.Initialize
End Sub

Public Sub Add (Text As String)
mList.Add(Text)
End Sub

Public Sub Contains (Text As String) As Boolean
Return mList.IndexOf(Text) > -1
End Sub[/CODE]
 

Attachments

Code:
    Dim lis As List = Array As String("one","three","five")
    Dim words() As String = Array As String("one", "two", "three", "four", "five")
    For Each word As String In words
        Log("List contains <" & word & "> = " & lis.As(JavaObject).RunMethod("contains",Array(word)))
    Next
 
"Ma che state a di'?" (Roman dialect; "What are you "saying" (writing)?").

I'm certainly not looking for a solution, which is just one line

it's the request for the (very easy) addition of the Contains function to the List class.
Of course your one line code is the best but I read that you don't like the IndexOf keyword and prefer Contains.

While waiting your wish comes true, WebQuest propose his way.
My solution is actually a response to his code. Lol.
 
B4X is making more sense than some other languages.
My practice is not to write a long one-line code which may appear cool to some programmer but not me.
If for some reasons 1 line of code is a bit difficult to read due to too many operators which is hurting my brain cells, just make it 2 lines.

B4X:
Dim contain As Boolean = SomeList.IndexOf("Open a new 'Wish'") > - 1
Button1.Enabled = contain

or better, 3 lines ;)
B4X:
Dim occur As Int = SomeList.IndexOf("Open a new 'Wish'")
Dim enable As Boolean = occur > -1
Button1.Enabled = enable
 
Back
Top