B4A Library String Functions

JonPM

Well-Known Member
Licensed User
Longtime User
Recently upgraded to B4A 1.8 and now when I compile apps created pre-1.8 I get error "Error description: Sub Process_Globals is missing." in this code module. So I added it and it works fine now...
 

Theera

Well-Known Member
Licensed User
Longtime User
Pls add this function: Instr(Text as string,Token as string) as int

Thank you for developed many fuction for manage string . Now I 'm looking for Instr() function Please help me
Best Regards
Theera

 

rbsoft

Active Member
Licensed User
Longtime User
B4X:
Sub InStr(StrVar As String,SearchStr As String)As Long 
   Dim x As Long 
   
   x = StrVar.IndexOf(SearchStr)
   Return x
End Sub


Rolf
 

Theera

Well-Known Member
Licensed User
Longtime User
Help me please

B4X:
Sub InStr(StrVar As String,SearchStr As String)As Long 
   Dim x As Long 
   
   x = StrVar.IndexOf(SearchStr)
   Return x
End Sub


Rolf

Thank You Rbsoft,for help and now I have question how to use s.bas file ? I have added s.file in program b4a already. I need use function in my program. But have error . This is
Compiling Code
Error parsing program
Error description:Sub Process_Globals is missing

P.S.
I created new function in your s.bas file. This is my function
Sub StrCutTokenStrAll(Passedstring As String,TokenStr As String) As String
Do Until Instr(Passedstring, TokenStr) = 0
Passedstring = s.Left(Passedstring, s.Instr(Passedstring, TokenStr) - 1) & s.Mid(Passedstring, s.Instr(Passedstring, TokenStr) + 1, s.Len(Passedstring) - s.Instr(Passedstring, TokenStr))
Loop
Return Passedstring
End Sub

I use check condition is s.StrCutTokenStrAll("This is a book"," ")=s.StrCutTokenStrAll("Thisisabook"," ") return true
 
Last edited:

Jost aus Soest

Active Member
Licensed User
Longtime User
@Theera:
Read the error decription: Sub Process_Globals is missing

You need a Process_Globals sub, even if it's empty!
 

rbsoft

Active Member
Licensed User
Longtime User
Exactly. If you add a new code module in B4A you always will find this code at the beginning of the code:

B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Just add this the code module (s.bas) and you can call the functions without problems from other modules. Example:

B4X:
Str1 = "abcdefgh"
Str2 = "c"
x = InStr(Str1,Str2)

'Returns 3

Rolf
 

rbsoft

Active Member
Licensed User
Longtime User

There is no need to refer to s.bas if your sub is part of that code module. You need this reference only if you are accessing a subin s.bas from another module. Your Sub should look like this:

B4X:
Sub StrCutTokenStrAll(Passedstring As String,TokenStr As String) As String
   Do Until Instr(Passedstring, TokenStr) = 0
   Passedstring = Left(Passedstring, Instr(Passedstring, TokenStr) - 1) & _
      Mid(Passedstring, Instr(Passedstring, TokenStr) + 1, Len(Passedstring) - _
      Instr(Passedstring, TokenStr))
   Loop
   Return Passedstring
End Sub

You would call your sub then from another module like this:

B4X:
t = s.StrCutTokenStrAll(PassedStr, TokenStr)

Rolf
 

Theera

Well-Known Member
Licensed User
Longtime User
@Theera:
Read the error decription: Sub Process_Globals is missing

You need a Process_Globals sub, even if it's empty!

Hi Jost aus Soest,
My app still has Sub Process_Globals already. I don't delete it. Please tell me
how I do continue in Sub Process_Globals.
Best Regards
Theera
 

Theera

Well-Known Member
Licensed User
Longtime User

Thank you RbSoft,I think you answer me while I asking.
 

Theera

Well-Known Member
Licensed User
Longtime User
Thank you RbSoft,I think you answer me while I asking.

Hi Roft (RbSoft),
I still have the same error. I use B4A v1.9 ,please help again
Best Regards
Theera
 

Theera

Well-Known Member
Licensed User
Longtime User
Hi Rolf,
I think your instr() is unlike as same as instr() from VB6.0 that use 3 variables.
Best Regards
Theera

B4X:
Sub InStr(StrVar As String,SearchStr As String)As Long 
   Dim x As Long 
   
   x = StrVar.IndexOf(SearchStr)
   Return x
End Sub


Rolf
 

rbsoft

Active Member
Licensed User
Longtime User
That is right. I left out the first parameter (StartPos) which I never used. It is optional in VB and PB. This InStr always starts to search from the beginning of the string.

Rolf
 

rbsoft

Active Member
Licensed User
Longtime User
If you need that parameter StartPos then the Sub should be like this;

B4X:
Sub InStr(StartPos As Long, StrVar As String,SearchStr As String)As Long 
    Dim x As Long 
    
   x = StrVar.IndexOf2(SearchStr,StartPos)
    Return x
End Sub

Rolf
 

Theera

Well-Known Member
Licensed User
Longtime User
Hi Margret and Roft,
I have 2 function about manage string in VB 6.0 ,I need use in B4A . I cann't. Please help me to correct them I tried them ,have always error. this my code:

Sub StrCutTokenStrAll(Passedstring As String,TokenStr As String) As String
'This function to check 2 string be same data (i.e. "Book1" = "Book 1" )

Do Until InStr(Passedstring, TokenStr) = 0
Passedstring = Left(Passedstring, InStr(Passedstring, TokenStr) - 1) & Mid(Passedstring, InStr(Passedstring, TokenStr) + 1, Len(Passedstring) - InStr(Passedstring, TokenStr))
Loop
Return Passedstring
End Sub

Sub GetWord( StrVar As String,token As String, Nth As Int) As String
' This function returns the Nth token in a string
' Ex. msgbox GetWord("This is a test.", " ", 2) return "is"

Dim i As Int
Dim p As Int
Dim R As Int
Dim TargetStr As String
'*****************
TargetStr = " "
For i = 1 To Len(StrVar)
If Mid(StrVar, i, 1) = " " Then
If Mid(StrVar, i - 1, 1) <> " " Then
TargetStr = TargetStr + Mid(StrVar, i, 1)
End If
Else
TargetStr = TargetStr + Mid(StrVar, i, 1)
End If
Next
StrVar = Trim(TargetStr)

If Nth < 1 Then
Return ""
Exit
End If

R = 0
For i = 1 To Nth
p = R
R = InStr( p + 1,StrVar,token)
If R = 0 Then
If i = Nth Then
Return Mid(StrVar, p + 1, Len(StrVar) - p)
Else
Return ""
End If
Exit
End If
Next
Return Mid(StrVar, p + 1, R - p - 1)
End Sub



 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…