Vb.net 如何操纵字符串以创建一组电子邮件?
我收到了我的Cc字符串,其中包含一个字符串,该字符串使用;性格我想知道如何使用不同的VB函数来做到这一点。我需要绕着琴弦循环,直到没有更多的琴弦;代码中的字符。我还想将它们添加到我的对象OneWorXeMail中。但是我想我已经知道怎么做了,因为我已经添加了一些代码。有人,请帮忙。我真的想不起来了Vb.net 如何操纵字符串以创建一组电子邮件?,vb.net,Vb.net,我收到了我的Cc字符串,其中包含一个字符串,该字符串使用;性格我想知道如何使用不同的VB函数来做到这一点。我需要绕着琴弦循环,直到没有更多的琴弦;代码中的字符。我还想将它们添加到我的对象OneWorXeMail中。但是我想我已经知道怎么做了,因为我已经添加了一些代码。有人,请帮忙。我真的想不起来了 Dim sEmailAddress As String Dim iPos As Integer iPos = InStr(Me.Cc, ";") Dim iLen As Integer iLen
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
Dim语义地址作为字符串
将IPO作为整数进行调整
iPos=InStr(Me.Cc,“;”)
作为整数的Dim-iLen
iLen=Len(Me.Cc)
暗晶石
sPart=左(Me.Cc,10)
作为弦的暗淡的sPart2
sPart2=右(Me.Cc,4)
像弦一样暗的sPart3
sPart3=Mid(Me.Cc,6,2)
当ipo 0时你会做什么
'???
OneWorXeMail.AddToAddress(sEmailAddress)
环
如果???然后
'???
OneWorXeMail.AddToAddress(sEmailAddress)
如果结束
首先,我建议您熟悉InStr、Len、Left、Right和Mid的.Net等价物,因为这些都是旧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
Dim nIndex1为Int32=0'子字符串函数从零开始,因此我们从这里开始
Dim nIndex2作为Int32=Me.CC.Text.IndexOf(“;”)查找文本中的第一个分号
'循环,直到找不到更多分号
直到nIndex2=-1为止
oNetworxEmail.AddToAddress(Me.CC.Text.Substring(nIndex1,nIndex2-nIndex1))'将电子邮件地址添加到oNetworxEmail对象
nIndex1=nIndex2+1'将第一个索引设置为最后一个分号之外的位置
nIndex2=Me.CC.Text.IndexOf(“;”,nIndex1)”搜索最后一个分号之后的下一个分号
环
'如果字符串末尾没有分号,请添加最后一个电子邮件地址
如果nIndex1
更新:代码已更新,以支持文本末尾不带分号的字符串。以下是一种方法,您可以通过循环字符来完成此操作,一次一个
' 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
此时,电子邮件
将具有拆分的电子邮件地址:
joe@bob.com
harry@windsor.com
barack@whitehouse.gov
您只需在它们上循环即可完成所需操作(如果需要,您可以将其集成到上述代码中):
更新:为什么事情如何做很重要的例子。这些是对这些字符串操作答案的粗略处理时间(将电子邮件数量增加到~800后) 我的犹太区回答:230ms 小乔伊·乔·沙巴多的回答:120ms 马克的回答:65ms 总之,不要用我的方法。呵呵
我不认为这比其他人的答案要好。这只是使用基本检查检查数组或对象集的另一个示例。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
Dim sCC As String=”one@two.com;three@four.net;five@six.gov"
Dim nStartPos作为整数=0
Dim nEndPos作为整数=0
Dim B以布尔值开头=False
将电子邮件作为新列表(字符串的列表)
尝试
对于i,整数=0到sCC.长度
如果开始的话
b开始=错误
nStartPos=i
如果结束
在这里使用OrElse,因为我不希望它以sCC.长度评估sCC(I)
如果i=sCC.长度或LSE sCC(i)=“;”则
nEndPos=i
如果结束
如果是0,那么
lstmails.Add(sCC.子字符串(nStartPos、nEndPos-nStartPos))
nStartPos=nEndPos
nEndPos=0
b开始=正确
如果结束
下一个
特例
MessageBox.Show(例如Message)
结束尝试
对于j,整数=0到1。计数-1
MessageBox.Show(电子邮件(j))
下一个
只需要使用Substring、Add、Length和Count方法。子字符串也可以避免,但会变得更混乱
p、 避免使用旧的VB函数,如“Mid”和“Left”。您应该使用较新的.NET等价物。您不能只使用String.Split吗?我忘了提到,不,我需要知道如何使用它way@TheFool为什么?作业使用String.Split.This更容易。也许你们两个可以一起工作?;-)@做记号是的,就是我。同一个人。没有人回应,这是我的家庭账户。你想知道真相吗?我在做我的第一份编程工作,我感觉自己快要淹死了。所以是的。我需要帮助。非常感谢。好的,我会的。非常感谢你。我也明白!此外,如果这对您有效,请单击答案旁边的复选标记接受答案,它将变为绿色。这将让其他人知道您已经找到了问题的答案,不再需要此问题的帮助。我认为如果没有最终的
,这可能会错过最后一个条目代码>角色。我认为如果只有一封邮件就不行了。谢谢大家的评论。代码已更新以解决这些问题。谢谢Mark。我很感激你花了这么多时间。它确实帮助了我,因为一旦我看到它,我就能理解它,然后我就能自己去做。我会把绳子给你的。明天我自己动手吧!
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