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.
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
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
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
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
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