Repeating a List Using ForEach

I have a list like this

Dim emailList as new List(Of String) emailList.Add(" one@domain.com ") emailList.Add(" two@domain.com ") emaillist.Add(" three@domain.com ") 

How can I iterate over a list using ForEach to get a single line with such emails

 one@domain.com ; two@domain.com ; three@domain.com 
+4
source share
5 answers

I'm not sure why you want to use foreach instead of String.Join expression. You can simply String.Join () the list, using a comma as a colon.

 String.Join(";", emailList.ToArray()) 
+8
source

You may try

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

Take a look at the String.Join Method

+2
source

I would not use the ForEach loop for this. Here is what I will do:

 String.Join(";", emailList.ToArray()); 
+2
source
  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 
+1
source
 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 handy helper.

0
source

Source: https://habr.com/ru/post/1302792/


All Articles