iterating a list with ForEach
I have a list like this
Dim emailList as new List(Of 开发者_JS百科String)
emailList.Add("one@domain.com")
emailList.Add("two@domain.com")
emaillist.Add("three@domain.com")
How can I iterate the list with ForEach to get one string with the emails like this
one@domain.com;two@domain.com;three@domain.com
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())
You can try
Dim stringValue As String = String.Join(";", emailList.ToArray)
Have a look at String.Join Method
I wouldn't actually use a ForEach loop for this. Here is what I would do:
String.Join(";", emailList.ToArray());
Dim emailList As New List(Of String)
emailList.Add("one@domain.com")
emailList.Add("two@domain.com")
emailList.Add("three@domain.com")
Dim output As StringBuilder = New StringBuilder
For Each Email As String In emailList
output.Append(IIf(String.IsNullOrEmpty(output.ToString), "", ";") & Email)
Next
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.
精彩评论