How can i split a String and use them in a Loop in VB?
-
12-12-2019 - |
Question
i get a string whith email adresses, seperated by a ";" which look like this:
geve@krag.de;;;Tobias@nxs.de;Wissel@weg.de;Sand@nex.de;Claudia@bea.de;;
i want to send an appointment to these email adresses here a sample to one person:
Dim appointment As New EWS.Appointment(esb)
appointment.Subject = "Einladung zu einem Termin"
appointment.Body = txtThema.Text
appointment.Start = Von
appointment.End = Bis
appointment.Location = "Raum 202 Kleinostheim"
appointment.RequiredAttendees.Add("geve@krag.de") // HERE ARE THE Attendees
appointment.Save(EWS.SendInvitationsMode.SendToAllAndSaveCopy)
i need every email adresses exept the first, becuase he sends the mails. how can i do that?
thanks in advance for your help
Solution
Here's how you would actually split the string into a string array:
Dim emails As String() = emailString.Split({";"}, StringSplitOptions.RemoveEmptyEntries)
There are other versions of the overloaded "Split" method, but that particular one lets you to pass in a StringSplitOptions value, allowing you to rule out blank entries right away.
After you have the string array, you can loop through and omit the first one in a few different ways.
We could use a For loop and skip the first entry entirely:
' Regular For loop approach
Dim emails As String() = emailString.Split({";"}, StringSplitOptions.RemoveEmptyEntries)
Dim appointment As New EWS.Appointment(esb)
With appointment
.Subject = "Einladung zu einem Termin"
.Body = txtThema.Text
.Start = Von
.End = Bis
.Location = "Raum 202 Kleinostheim"
End With
' start at i = 1 to skip index 0
For i = 1 To emails.Length - 1
appointment.RequiredAttendees.Add(emails(i)) ' HERE ARE THE Attendees
Next
appointment.Save(EWS.SendInvitationsMode.SendToAllAndSaveCopy)
Or identify the sender's email and use a For Each with an If to omit it:
' For Each approach
' set up string array and appointment object like above
Dim sender As String = emails.FirstOrDefault()
' ignore all instances of the sender's address
For Each address In emails
If address.ToLower <> sender.ToLower Then
appointment.RequiredAttendees.Add(emails(i)) ' HERE ARE THE Attendees
End If
Next
appointment.Save(EWS.SendInvitationsMode.SendToAllAndSaveCopy)
I'd say play around with it though, and use the approach that best suits you.
OTHER TIPS
You can use the split function to parse out each email and use Linq to remove blanks and Skip the first entry. I think this syntax will work in VB.
Dim emailString = "geve@krag.de;;;Tobias@nxs.de;Wissel@weg.de;Sand@nex.de;Claudia@bea.de;;"
Dim emaillist = (From email In emailString.Split(";").Skip(1) Where email.Length > 0 Select email).ToArray()