-4

vb.net 中是否有任何方法可以从电子邮件地址获取用户名?

例如,我有一个字符串变量,它由 value 组成tracywilly@gmail.com,这里我需要获取用户名,即tracywilly我的预期输出

Dim email_addr as string
Dim usr as string
email_addr = "tracywilly@gmail.com"
4

1 回答 1

5

split()您可以使用或 regex在 vb.net中实现此目的

看下面的方法

1.)split()功能:

 Function username(ByVal str As String) As String
        Dim arStr As String()
        arStr = str.Split("@")
        username = arStr(0)
        Return username
    End Function

2.)regex

Function username_1(ByVal str As String) As String
    username_1 = System.Text.RegularExpressions.Regex.Match(str, "^.*?(?=@)").ToString
    Return username_1
End Function

它是如何工作的?

Dim email_addr as string
Dim usr as string
email_addr = "tracywilly@gmail.com"
usr = username(email_addr)
usr = username_1(email_addr)

Live Demo

于 2015-12-15T09:15:19.203 回答