Android Question Merge list

apty

Active Member
Licensed User
Longtime User
i have a list that looks like below:
B4X:
abcd
efgh
ijkl

I want to merge this list and have a single string joined with comma like below:
B4X:
abcd,efgh,ijkl
Please assist.
 

Mahares

Expert
Licensed User
Longtime User
I want to merge this list and have a single string joined with comma like below:
Maybe something simple like this:
B4X:
Dim MyList As List   
    MyList.Initialize   
    MyList.Add("abcd")
    MyList.Add("efgh")
    MyList.Add("ijkl")
    Dim str As String
    For i=0 To MyList.Size-1
        str=str & MyList.Get(i) & ","
    Next
    str=str.SubString2(0,str.Length-1)
    Log(str)  'displays: abcd,efgh,ijkl
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I recommend you to watch the strings tutorial: https://www.b4x.com/etp.html

B4X:
Sub ListToString(list As List) As String
 If list.Size = 0 Then Return ""
 Dim sb As StringBuilder
 sb.Initialize
 For Each s As String in list
   sb.Append(s).Append(",")
 Next
sb.Remove(sb.Length - 1, sb.Length)
Return sb.ToString
End Sub
Code will work the same in B4A, B4i and B4J.
 
Upvote 0
Top