A Function to extract the "name part" of an email

A handy little function to remove the name part of an email address.

Public Class Form1
    Public Function getNameFromEmail(ByVal strEmail As String) As String
        ' This purpose of this function is to return the
        ' name part of an email address. For instance
        ' joe@somewhere.com would return just joe.

        ' Use the IndexOf to find out how many characters
        ' in the @ sign is so I can use that number later
        ' to determine the amount of characters to select
        ' in the substring. 

        Dim i As Integer = strEmail.IndexOf("@")
        Dim newstring As String = strEmail.Substring(0, i)
        Return newstring
    End Function

    Private Sub Button1_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        Dim emailArray() As String = { _
            "me@somewhere.com", _
            "you@anywhere.com", _
            "them@nowhere.com"}

        For Each emailName As String In emailArray
            Dim strMess As String = CStr(getNameFromEmail(emailName))
            MessageBox.Show(strMess, "Email Name Test", _
            MessageBoxButtons.OK)
        Next
    End Sub
End Class

Share and Enjoy:
  • Print
  • Facebook
  • Google Bookmarks

Comments are closed.