我将如何操作字符串以创建一组电子邮件? [英] How would I manipulate a string in order to create a set of emails?
问题描述
我在我的 Cc 字符串中收到了一个字符串,该字符串使用 ; 分隔电子邮件.特点.我想知道如何使用不同的 VB 函数来做到这一点.我需要环绕字符串直到没有更多;代码中的字符.我还想将它们添加到我的对象 oNetworxEmail.但我想我已经知道如何做到这一点,因为我已经添加了一些代码.有人请帮忙.实在想不通
I have received in my Cc string which contains a string which separates emails using the ; character. I would like to know how to use the different VB functions to do this. I need to loop around the strings until there are no more ; characters in the code. And I would also like to add them to my object oNetworxEmail. But I think I know how to do this already as I have added some code already. Someone, please help. I really can't get my head around it
Dim sEmailAddress As String
Dim iPos As Integer
iPos = InStr(Me.Cc, ";")
Dim iLen As Integer
iLen = Len(Me.Cc)
Dim sPart As String
sPart = Left(Me.Cc, 10)
Dim sPart2 As String
sPart2 = Right(Me.Cc, 4)
Dim sPart3 As String
sPart3 = Mid(Me.Cc, 6, 2)
Do While iPos <> 0
'???
oNetworxEmail.AddToAddress(sEmailAddress)
Loop
If ??? Then
'???
oNetworxEmail.AddToAddress(sEmailAddress)
End If
推荐答案
这是一种您可以通过循环遍历字符来实现的方法,一次一个.
Here's a way you could do it by looping through the characters, one at a time.
' Code assumes that Option Infer On
Dim sEmailAddress = "; joe@bob.com;harry@windsor.com;; barack@whitehouse.gov ; ;"
' A list to hold the separate email addresses
Dim emails = New List(Of String)()
' A string builder used to build up the current email address
Dim currentEmail = New StringBuilder()
' Loop through each character in the source string
For Each c In sEmailAddress
Select Case c
Case ";"c
' We found the delimiter. If the current email is not empty
' then we will add it to the list.
If currentEmail.Length > 0 Then
emails.Add(currentEmail.ToString())
End If
' Clear out the buffer for the next email
currentEmail.Clear()
Case " "c
' We will ignore spaces, since they aren't valid in an email address
Case Else
' Append the current character to the current email address
currentEmail.Append(c)
End Select
Next
' Add the last email address to the list, if any
If currentEmail.Length > 0 Then
emails.Add(currentEmail.ToString())
End If
此时 emails
将具有拆分的电子邮件地址:
At this point emails
will have the split email addresses:
joe@bob.com
harry@windsor.com
barack@whitehouse.gov
您可以循环遍历它们以执行您想要的操作(如果需要,您可以将其集成到上面的代码中):
You can just loop over them to do what you want (you could integrate this into the above code, if you wanted):
For Each email In emails
oNetworxEmail.AddToAddress(email)
Next
这篇关于我将如何操作字符串以创建一组电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!