views:

73

answers:

5

I have a list like this

Dim emailList as new List(Of String)
emailList.Add("[email protected]")
emailList.Add("[email protected]")
emaillist.Add("[email protected]")

How can I iterate the list with ForEach to get one string with the emails like this

[email protected];[email protected];[email protected]
+1  A: 
Dim emailList As New StringBuilder()

For Each (email As String In emails)
     emailList.Append(String.Format("{0};", email))
Next

Return emailList.ToString()

Forgive me if there are any syntax errors ... my VB.NET is a little rusty and I don't have a complier handy.

Scott Vercuski
@Dan - thank you !
Scott Vercuski
+6  A: 

I'm not sure why you would want to use a foreach instead of a String.Join statement. You could simply String.Join() the list using a semi-colon as your joining character.

String.Join(";", emailList.ToArray())
NebuSoft
@NebuSoft - I completely forgot about this ... I must be getting old ... +1 for you ... I withdraw my answer.
Scott Vercuski
that wouldn't work. The second parameter is a string array, not a generic list.
Gabriel McAdams
I apologize, I haven't used VB since VB6. In C# you can take the generic list to an array (emailList.ToArray())...I'm sure there is an equivalent via VB.NET
NebuSoft
is there a way to use this mechanism if the list of of integers?
rich
+2  A: 

You can try

Dim stringValue As String = String.Join(";", emailList.ToArray)

Have a look at String.Join Method

astander
+2  A: 

I wouldn't actually use a ForEach loop for this. Here is what I would do:

String.Join(";", emailList.ToArray());
Gabriel McAdams
A: 
       Dim emailList As New List(Of String)
    emailList.Add("[email protected]")
    emailList.Add("[email protected]")
    emailList.Add("[email protected]")

    Dim output As StringBuilder = New StringBuilder
    For Each Email As String In emailList
        output.Append(IIf(String.IsNullOrEmpty(output.ToString), "", ";") & Email)
    Next
rich