Android Code Snippet VB like Join functions.

Just like VB.NET, Should be fine in any b4x language.

Name: Join, Join2
Description: Joins a list or array in a single string.

B4X:
'--- joins a list together as a string
'--- if you do not want a join char then pass in ""
Public Sub Join(lst As List, joinChar As Object) As String
 
    Dim retStr As StringBuilder
    retStr.Initialize
 
    For Each str In lst
        retStr.Append(str).Append(joinChar) 
    Next
 
    Return retStr.ToString
 
End Sub

'--- joins a array together as a string
'--- if you do not want a join char then pass in ""
Public Sub Join2(arr() As String, joinChar As Object) As String
 
    Dim retStr As StringBuilder
    retStr.Initialize
 
    For Each str In arr
        retStr.Append(str).Append(joinChar) 
    Next
 
    Return retStr.ToString
 
End Sub

Dependencies: None
Tags: String
 

emexes

Expert
Licensed User
Standing on the OP's shoulders, here's one that doesn't have an extraneous trailing separator (and separator can be String, not just single Char)

In fact, I'm now wondering what "" translates to as a Char - is it "no character" or is it Chr(0)?

B4X:
'--- joins a list together as a string
'--- if you do not want a separator/join char then pass in ""
Public Sub Join(Lst As List, Separator As Object) As String
 
    If Lst.Size = 0 then    'pessimists of the world, unite!
        Return ""
    End if

    Dim Joined As StringBuilder
    Joined.Initialize
 
    Joined.Append(Lst.Get(0))
    For I = 1 to Lst.Size - 1     'skips first item since already done
        Joined.Append(Separator).Append(Lst.Get(I))
    Next I

    Return Joined.ToString
 
End Sub
 
Top