B4A Library String Functions

JonPM

Well-Known Member
Licensed 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
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

Hello All,

*** Just Added These Three New Functions: August 25th, 2011 - 1.10pm

s.Proper() – Example: converts “jerry smith” to “Jerry Smith” or "ms. betty sue smith" to "Ms. Betty Sue Smith"
s.Lower()
s.Upper()

*** Just Added These Three New Functions: July 28th, 2011 - 12.27am

s.Space()
s.RndChrGen() - Generate random characters to fill Edit.Text, etc.
s.Exist()

*** Just Added These Two New Functions: July 24th, 2011 - 2.27am

s.Empty()
s.iif() - (Immediate If - very useful)
Example: MyVar = s.iif(File.ExternalWritable, File.DirRootExternal, File.DirInternal)

I have now been writing in B4A for just over three weeks. Each day I learn something new and how powerful it is! But, at first I was lost and did not know how flexable it is but was still missing some string functions I used all the time in VB and others like Visual Fox. I have put together a sample of some UDFs I wrote and wanted to share them as so many others here have done! I hope it is useful for someone. The new functions are listed below:

s.At()
s.Left()
s.Len()
s.Ltrim()
s.Mid()
s.Right()
s.Rtrim()
s.Stuff()
s.Trim()
s.Val()



Thanks,

Margret
 

rbsoft

Active Member
Licensed 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
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:

rbsoft

Active Member
Licensed 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
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
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
@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
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
Thank you RbSoft,I think you answer me while I asking.
 

Theera

Well-Known Member
Licensed 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
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
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
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
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



Hello All,

*** Just Added These New Functions: April 6th, 2012 - 10.00pm

Attached to Bottom of This Post: Version 1.2




Corrected for B4A version 1.9

B4X:
AsciiCodes(),  SearchDate(),  DateNew(),  DateNOD(),  Date(),  Split(),  InStr(),  MB(),  ValidDate()
   
AsciiCodes - 'Displays an dialog showing all the Ascii Codes
Date - 'returns the current system date
SearchDate("04/06/2012", "04/14/2012", 30) - 'true means the dates passed are within (x) days of each other, pass x as 3rd parameter
DateNew("02/12/2012", 90) - 'returns a new date 90 days later, 2nd parameter can be + or - number
DateNOD("04/06/2012", "12/12/2011") - 'show the days passed between dates passed
ValidDate("13/12/2012") - 'returns true if you pass a valid date
InStr("Where is me at", "me") - 'shows the location of 2nd parameter in string (RBsoft)
Split("Here is a test string to take apart", " ") - 'returns the string split into List elements at each occurance of the 2nd parameter (RBsoft)   
MB("My Text Here") - 'same as MsgBox() but only the one parameter is needed
*** Just Added These Three New Functions: August 25th, 2011 - 1.10pm

s.Proper() – Example: converts “jerry smith” to “Jerry Smith” or "ms. betty sue smith" to "Ms. Betty Sue Smith"
s.Lower()
s.Upper()

*** Just Added These Three New Functions: July 28th, 2011 - 12.27am

s.Space()
s.RndChrGen() - Generate random characters to fill Edit.Text, etc.
s.Exist()

*** Just Added These Two New Functions: July 24th, 2011 - 2.27am

s.Empty()
s.iif() - (Immediate If - very useful)
Example: MyVar = s.iif(File.ExternalWritable, File.DirRootExternal, File.DirInternal)

I have now been writing in B4A for just over three weeks. Each day I learn something new and how powerful it is! But, at first I was lost and did not know how flexable it is but was still missing some string functions I used all the time in VB and others like Visual Fox. I have put together a sample of some UDFs I wrote and wanted to share them as so many others here have done! I hope it is useful for someone. The new functions are listed below:

s.At()
s.Left()
s.Len()
s.Ltrim()
s.Mid()
s.Right()
s.Rtrim()
s.Stuff()
s.Trim()
s.Val()



Thanks,

Margret
 
Last edited:
Top