Android Tutorial Implode - Converting String Array to String

Needed this for my program, maybe it will be of some use to others, too:

B4X:
Sub Implode(glue As String, pieces() As String)
   Dim result As StringBuilder

   If pieces.Length = 0 Then Return ""
   If pieces.Length = 1 Then Return pieces(0)
   
   result.Initialize
   result.Append(pieces(0))
   For i=1 To pieces.Length-1
      result.Append(glue & pieces(i))
   Next
   Return result.ToString
End Sub

Use it like this:
B4X:
Dim S() As String = Array As String("A", "B", "C")
Log(Implode("|", S))
The example outputs the following:
B4X:
A|B|C
 

sioconcept

Active Member
Licensed User
Longtime User
The same for List

B4X:
Sub Implode(Glue As String, pieces As List)
   If pieces.Size = 0 Then Return ""
   Dim I As Int
   Dim Result As String
   For I = 0 To pieces.Size - 1
      Result = Result & pieces.Get(I) & Glue
   Next
   Return Result.SubString2(0, Result.LastIndexOf(Glue))
End Sub
 
Top