B4A Library String Functions

Jost aus Soest

Active Member
Licensed User
Longtime User
You could also use the split-function to get the nth word:
B4X:
Dim s As String
s = "The quick brown fox jumped over the lazy old dog."
word2 = Split(s, " ")(2)  '<-- "brown"
word3 = Split(s, " ")(3)  '<-- "fox"
 

Jost aus Soest

Active Member
Licensed User
Longtime User
getWord

And this is the complete getWord-function:
B4X:
Sub getWord(str As String, Delimiter As String, n As Int) As String 
  Return Regex.Split(Delimiter, str)(n)
End Sub
 

margret

Well-Known Member
Licensed User
Longtime User
Hello,

I am going to add this function but as [SplitGetWord]. I love the one liners like you wrote above but it returns one word above what you would expect. If you want the 2nd word and you pass a 2, it returns the 3rd word. If you pass a nth selection greater than the array, it throws an exception. I used RBsofts function and modified it with yours to create the [SplitGetWord], if this is ok. The new function is:

B4X:
Sub SplitGetWord(CurrentString As String, Split_At_Delimiter As String, GetElement As Int) As String
   '*** Original Function by RBSoft modified with suggestions from Jost aus Soest
   Dim t As List
   t = Regex.Split(Split_At_Delimiter, CurrentString)
   rs = iif(GetElement-1 <= t.Size -1, t.Get(GetElement-1), "Invalid Element Selected")
   Return rs
End Sub
 

Jost aus Soest

Active Member
Licensed User
Longtime User
I love the one liners like you wrote above but it returns one word above what you would expect.

I have choosen this counting, because in B4A and Java arrays and string positions are working in the same way: from 0 to n-1.
BTW: Personaly I would prefer start counting from 1.

Of course you can implement this function in your module as you like!
 

Jost aus Soest

Active Member
Licensed User
Longtime User
My Easter present for you, Margret, a reverse function for Split, to join strings:
B4X:
Sub Join(Strings() As String, Delimiter As String) As String

   Dim i As Int
   Dim sb As StringBuilder

   sb.Initialize
   sb.Append(Strings(0))
   For i = 1 To Strings.Length - 1
      sb.Append(Delimiter).Append(Strings(i))
   Next'i
   Return sb.ToString

End Sub
 
Last edited:

BarrySumpter

Active Member
Licensed User
Longtime User
Farreakin' Aye man!
Thanks all for the contribution.
Very nice!


Edit:
Anything like:

dt = DateTime.format("YYYY-MM-DD HH:MM:SS", CurrentDate)
I think that's a sql date display format. So may already exist.

or

Have always wanted this:

myExtract = s.ExtractBetween("This is a test", "This ", " test")
returns: "is a"

tia
 
Last edited:

margret

Well-Known Member
Licensed User
Longtime User
Here it is. Copy it to your s. code module:

B4X:
Sub ExtractBetween(CurrentString As String, FirstWord As String, LastWord As String) As String
   Split_At_Delimiter = Chr(32)
   Dim t As List
   t = Regex.Split(Split_At_Delimiter, CurrentString)
   Dim newstr As String : st = 0 : ed = 0 : newstr = ""
   For l = 0 To t.Size -1
      If t.Get(l) = FirstWord Then
         st = l
      End If
      If t.Get(l) = LastWord Then
         ed = l
      End If   
   Next
   For l = (st +1) To (ed -1)
      newstr = newstr & t.Get(l) & " "
   Next
   Return newstr.Trim
End Sub
 

BarrySumpter

Active Member
Licensed User
Longtime User
This is mine (noobie) with proof of work:
B4X:
'Activity module
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)

    Dim myVar As String
    
    myVar = "This is a test"
    
    Dim myPos As Int
    
    
    myVar = ExtractBetween("This is a test.  This is only a test. For the next sixty seconds.","only", "sixty")
    myVar = ExtractBetween("<tag1>This is a test.</tag1>  <tag2>This is only a test.</tag2> <tag3>For the next sixty seconds you will be sujected to intense radiation.</tag3>", "<tag1>", "</tag1>")
    myVar = ExtractBetween("<tag1>This is a test.</tag1>  <tag2>This is only a test.</tag2> <tag3>For the next sixty seconds you will be sujected to intense radiation.</tag3>", "<tag2>", "</tag2>")
    myVar = ExtractBetween("<tag1>This is a test.</tag1>  <tag2>This is only a test.</tag2> <tag3>For the next sixty seconds you will be sujected to intense radiation.</tag3>" ,"<tag3>", "</tag3>")
    
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub ExtractBetween(ExtractFrom As String, StartingWith As String, EndingAt As String) As String
        
        If ExtractFrom.Length = 0 Then
            Return "Nothing to Extract"
        End If
        
        'Sub Mid(Text As String, Start As Int, Length As Int) As String 

        If InStr(ExtractFrom, StartingWith) > -1 Then
        Else
            Return StartingWith & " - not found"
        End If
        
        If InStr(ExtractFrom, EndingAt) > -1 Then
        Else
            Return EndingAt & " - not found"
        End If
        
        Dim myStartPos As Int
        myStartPos = InStr(ExtractFrom, StartingWith)        
        myStartPos = myStartPos + StartingWith.Length + 1
        
        Dim myLength As Int
        myLength = ExtractFrom.Length - myStartPos + 1
                
        Dim myVarWithLeftRemoved As String
        myVarWithLeftRemoved = Mid(ExtractFrom, myStartPos, myLength)
                
        myStartPos = 1        
        myLength = InStr(myVarWithLeftRemoved, EndingAt)
            
        Dim myVarWithRightRemoved As String
        'Sub Left(Text As String, Length As Long)As String
        myVarWithRightRemoved = Left(myVarWithLeftRemoved, myLength )
                
        Dim myVar As String
        myVar = Trim(myVarWithRightRemoved)
        Msgbox ("*" & myVar & "*","")
        
        Return myVar
                
End Sub

Sub InStr(StrVar As String,SearchStr As String)As Long    'Same as At()
    '*** Function by RBSoft
    Dim x As Long
    x = StrVar.IndexOf(SearchStr)
    Return x
End Sub
Sub MB(Message As String)
    Msgbox(Message, "")
End Sub
Sub Left(Text As String, Length As Long)As String 
    If Length>Text.Length Then Length=Text.Length 
    Return Text.SubString2(0, Length)
End Sub
Sub Right(Text As String, Length As Long) As String
    If Length>Text.Length Then Length=Text.Length 
    Return Text.SubString(Text.Length-Length)
End Sub
Sub Mid(Text As String, Start As Int, Length As Int) As String 
    If Len(Text) = 0 Then
        Return ""
    End If    
    Return Text.SubString2(Start-1,Start+Length-1)
End Sub
Sub Len(Text As String) As Long
    Return Text.Length
End Sub
Sub Trim(Text As String) As String
    Return Text.Trim
End Sub
 
Last edited:

Theera

Well-Known Member
Licensed User
Longtime User
Thank you for kind of you.
Theera
 

texwillerx

Member
Licensed User
Longtime User
instr function

Thank you very much for this excellent work.

Does instr work same as VB version? Should we use

x = StrVar.IndexOf(SearchStr)+1

since indexof returns a zero based result compared to 1 based result for VB
 

tdocs2

Well-Known Member
Licensed User
Longtime User
Fantastic Job

Dear Margret,
As a newbie to B4A but experienced in VB and other languages, I congratulate you on an excellent piece of coding on both examples.
Your style reflects simplicity, accuracy, and succintness - essential skills for an excellent software developer.
Thank you for your contributions.
Sandy
 

Bens HobbyCorner

New Member
Licensed User
Longtime User
S string already used....

Used your module on existing code, a string named S was used already.
Got an error message on the line

s=s.Replace((CRLF),"")

renamed the string s to r and after that everything is working.

I am a newbie, respect your work. Is this supposed to happen?

Have fun
Ben Zijlstra
 

fdx12345

Active Member
Licensed User
Longtime User
Complete List of Strings in Library

Is there a spot were I can get a complete list of the string commands? Looks like I have to find all of the individual updates to generate a list and I do not want to miss one. The string routines are great! Thank you.
 

margret

Well-Known Member
Licensed User
Longtime User
The command list is now on post #1 of this thread and these functions have been updated to a library.

Is there a spot were I can get a complete list of the string commands? Looks like I have to find all of the individual updates to generate a list and I do not want to miss one. The string routines are great! Thank you.
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…