views:

89

answers:

7

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 = "[email protected]|[email protected]"
Dim brokened() As String
brokened = Split(txtEmailFrom.Text, "|")

Dont know where to go from here...

A: 
txtEmailFrom.Text = "[email protected]|[email protected]"
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) = ....

shahkalpesh
A: 

if you can use LINQ

 txtEmailFrom.Text = "[email protected]|[email protected]"
 Dim result = txtEmailFrom.Text.Split(CChar("|")) _
              .Where(Function(d) d.Contains("domain1.com")).FirstOrDefault
Fredou
A: 
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
Joel Etherton
A: 

Hi there.

(I got interrupted writing my answer, so I'm determined to finish!)

You can try this:

Module Module1

    Sub Main()

        Dim email As String = "[email protected]|[email protected]"

        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.

Jason Evans
using firstOrDefault will not return an array or ienumeration
Fredou
@Fredou - Your right, I had mis-understood the requirements. Have changed the code accordingly.
Jason Evans
+2  A: 
For Each email In brokened
    If email.Contains("domain1.com") Then
        Return email
    End If
Next
Ragepotato
There's a time and place for LINQ and I'd say that this is not one of them, a good old FOR loop like you have works best here and can be more easily read by everyone else.
Chris Haas
A: 

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

pdiddy
A: 
Dim test As String = "[email protected]|[email protected]"
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
Nate Heinrich