我收到了我的Cc字符串,其中包含一个用以分隔电子邮件的字符串;字符。我想知道如何使用不同的VB函数来做到这一点。我需要在字符串周围循环,直到不再存在;代码中的字符。我还想将它们添加到我的对象oNetworxEmail中。但我想我已经知道如何做到这一点,因为我已经添加了一些代码。有人,请帮忙。我真的无法理解它
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
答案 0 :(得分:1)
首先,我建议你熟悉.Net等效的InStr,Len,Left,Right和Mid,因为这些都是旧VB的遗留功能。
其次,我会删除你的其他问题。在这个网站上多次提出同样的问题是非常不满的。
最后,这里是您不使用拆分功能的问题的答案:
Dim nIndex1 As Int32 = 0 ' The substring function starts at zero so we begin here
Dim nIndex2 As Int32 = Me.CC.Text.IndexOf(";") ' Find the first semi-colon in the text
' Loop until there are no more semi-colons found
Do Until nIndex2 = -1
oNetworxEmail.AddToAddress(Me.CC.Text.Substring(nIndex1, nIndex2 - nIndex1)) ' Add an email address to the oNetworxEmail object
nIndex1 = nIndex2 + 1 ' Set the first index to the position beyond the last semi-colon
nIndex2 = Me.CC.Text.IndexOf(";", nIndex1) ' Search for the next semi-colon beyond the last
Loop
' Add the last email address in the event that there is no semi-colon at the end of the string
If nIndex1 < Me.CC.Text.Length Then
oNetworxEmail.AddToAddress(Me.CC.Text.Substring(nIndex1, Me.CC.Text.Length - nIndex1))
End If
更新:代码已更新为支持在文本末尾没有分号的字符串。
答案 1 :(得分:1)
这是一种可以通过循环遍历字符来实现的方法。
' 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
将包含拆分电子邮件地址:
joe@bob.com
harry@windsor.com
barack@whitehouse.gov
您可以循环遍历它们以执行您想要的操作(如果需要,可以将其集成到上面的代码中):
For Each email In emails
oNetworxEmail.AddToAddress(email)
Next
答案 2 :(得分:0)
更新:为什么事情如何完成的例子很重要。这些是对这些字符串操作答案的粗略处理时间(在将电子邮件计数提高到~800之后)
我的贫民窟回答: 230ms
Joey Joe Joe Jr Shabadoo的回答: 120ms
Mark的回答: 65ms
简而言之,不要使用我的方法。嘿。
这绝不比其他人的答案好,我不认为。只是使用基本检查遍历数组或对象集的另一个示例。 String.Append可能会更好。老实说,我不知道速度差异。
另一种(更多贫民窟)的方式来做透视...
Dim sCC As String = "one@two.com;three@four.net;five@six.gov"
Dim nStartPos As Integer = 0
Dim nEndPos As Integer = 0
Dim bStarted As Boolean = False
Dim lstEmails As New List(Of String)
Try
For i As Integer = 0 To sCC.Length
If bStarted Then
bStarted = False
nStartPos = i
End If
'Using OrElse here, because I don't want it evaluating sCC(i) at sCC.Length
If i = sCC.Length OrElse sCC(i) = ";" Then
nEndPos = i
End If
If nEndPos <> 0 Then
lstEmails.Add(sCC.Substring(nStartPos, nEndPos - nStartPos))
nStartPos = nEndPos
nEndPos = 0
bStarted = True
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
For j As Integer = 0 To lstEmails.Count - 1
MessageBox.Show(lstEmails(j))
Next
只需要使用方法Substring,Add,Length和Count。也可以避免使用子串,但它会变得更加混乱。
P.S。避免使用旧的VB函数,如“Mid”和“Left”。您应该使用更新的.NET等效项。