String Split&搜索VB.NET

时间:2010-01-28 19:22:36

标签: .net asp.net vb.net string

需要在vb中取一个字符串并将其拆分。还需要查看两个返回的值并返回包含“domain1.com”的值。管道是分隔符。

txtEmailFrom.Text = "john@huno.com|james@domain1.com"
Dim brokened() As String
brokened = Split(txtEmailFrom.Text, "|")

不知道从哪里开始......

7 个答案:

答案 0 :(得分:2)

For Each email In brokened
    If email.Contains("domain1.com") Then
        Return email
    End If
Next

答案 1 :(得分:0)

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

我正在写这篇没有IDE&这可能是VB6风格 希望它能给你一个转换到VB.net的想法

编辑:当然,最好在此行if emailSplit(1) = ...之前添加对数组边界的检查。

答案 2 :(得分:0)

如果你可以使用LINQ

 txtEmailFrom.Text = "john@huno.com|james@domain1.com"
 Dim result = txtEmailFrom.Text.Split(CChar("|")) _
              .Where(Function(d) d.Contains("domain1.com")).FirstOrDefault

答案 3 :(得分:0)

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

答案 4 :(得分:0)

(我打断了我的答案,所以我决定完成!)

你可以试试这个:

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

希望这会有所帮助。 雅各

答案 5 :(得分:0)

我将拆分的电子邮件放在列表中并使用list.FindAll方法查找所有domain1.com

我打算用c#写这个,我对它比较熟悉,但对于vb.net应该是一样的

List<string> emails = new List<string>();
emails.AddRange(txtEmailFrom.Text.Split("|".ToCharArray()));

emails.FindAll(s=> {return s.Contains("domain1.com");} );

类似的东西......用IDE编写

答案 6 :(得分:0)

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