String Split & Search VB.NET
Need to take a string in vb and split it. Also need to look through the two returned values and return the value which contains "domain1.com". Pipelines are the delimiter.
txtEmailFrom.Text = "john@huno.com|james开发者_运维问答@domain1.com"
Dim brokened() As String
brokened = Split(txtEmailFrom.Text, "|")
Dont know where to go from here...
For Each email In brokened
If email.Contains("domain1.com") Then
Return email
End If
Next
txtEmailFrom.Text = "john@huno.com|james@domain1.com"
Dim brokened() As String
dim email as string
dim emailSplit() as string
brokened = Split(txtEmailFrom.Text, "|")
for email in brokened
emailSplit = Split(email, "|")
if emailSplit(1) = "domain1.com" then
Console.WriteLine(email)
end if
next
I am writing this without IDE & this could be VB6 style.
Hopefully, it should give you an idea of converting to VB.net
EDIT: Ofcourse, it will be better to add checks for array bounds before this line if emailSplit(1) = ...
.
if you can use LINQ
txtEmailFrom.Text = "john@huno.com|james@domain1.com"
Dim result = txtEmailFrom.Text.Split(CChar("|")) _
.Where(Function(d) d.Contains("domain1.com")).FirstOrDefault
I'd put the splited email in a list and use the list.FindAll method to find all domain1.com
I'm going to write this in c#, i'm more familiar with it, but it should be the same for vb.net
List<string> emails = new List<string>();
emails.AddRange(txtEmailFrom.Text.Split("|".ToCharArray()));
emails.FindAll(s=> {return s.Contains("domain1.com");} );
something like that ... writing witout IDE
Dim test As String = "john@huno.com|james@domain1.com"
Dim brokend() As String
brokend = test.Split(New String() {"|"}, StringSplitOptions.None)
For Each email As String In brokend
If email.EndsWith("domain1.com") Then
Return email;
End If
Next
Dim brokened() As String = txtEmailFrom.Text.Split("|"c)
Dim returnValue as String
For each item as String in brokened
If item.Contains("domain1.com") Then
returnValue = item
End If
Next
(I got interrupted writing my answer, so I'm determined to finish!)
You can try this:
Module Module1
Sub Main()
Dim email As String = "john@huno.com|james@domain1.com"
Dim brokened() As String
brokened = Split(email, "|")
Dim k As List(Of String) = (From j As String In brokened _
Where j.Contains("domain1.com") _
Select j).ToList()
For Each u As String In k
Console.WriteLine(u)
Next
End Sub
End Module
Hope this helps. Jas.
精彩评论