vb.net中有什么方法可以从电子邮件地址获取用户名?
例如,我有一个字符串变量,它包含value tracywilly@gmail.com
,在这里我需要获取用户名,即tracywilly
我的预期输出
Dim email_addr as string Dim usr as string email_addr = "tracywilly@gmail.com"
Vivek S... 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
您可以使用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