EmailParts.vbs

'=========================================================================
' EmailParts.vbs
' VERSION: 1.0
' AUTHOR: Brian Steinmeyer
' EMAIL: sigkill@sigkillit.com
' WEB: http://sigkillit.com
' DATE: 4/1/2012
' COMMENTS: Four methods to break an email address into the local and
' domain parts. I prefer the string methods because you can make them
' 1-liners, doesn't require additional variables, and simplifies error
' handling.  
'=========================================================================
Option Explicit

Call EmailParts1("john.doe@domain.com")
Call EmailParts2("john.doe@domain.com")
Call EmailParts3("john.doe@domain.com")
Call EmailParts4("john.doe@domain.com")

'METHOD1(String) - Left/Right with Instr
Sub EmailParts1(email)
    Wscript.Echo "METHOD 1:" & vbCrLf & _
    "Email: " & email & vbCrLf & _
    "Local: " & Left(email,InStr(1,email,"@",1)-1) & vbCrLf & _
    "Domain: " & Right(email,(Len(email)-InStr(1,email,"@",1))+1)  
End Sub

'METHOD2(String) - Left/Right with InstrRev
Sub EmailParts2(email)
    Wscript.Echo "METHOD 2:" & vbCrLf & _
    "Email: " & email & vbCrLf & _
    "Local: " & Left(email,InstrRev(email,"@")-1) & vbCrLf & _
    "Domain: " & Right(email,(Len(email)-Instr(email,"@"))+1)
End Sub

'METHOD3(String) - Mid with InStr
Sub EmailParts3(email)
    Wscript.Echo "METHOD 3:" & vbCrLf & _
    "Email: " & email & vbCrLf & _
    "Local: " & Mid(email, 1, InStr(email, "@")-1) & vbCrLf & _
    "Domain: " & Mid(email,InStr(email,"@"),Len(email)-(InStr(email,"@")-1))
End Sub

'METHOD4(Array) - Split dumps into an array (NOT RECOMMENDED)
Sub EmailParts4(email)
    Dim arrEmail: arrEmail = Split(email,"@")
    arrEmail(1) = "@" & arrEmail(1) 'Need to add @ back in
    Wscript.Echo "METHOD 4:" & vbCrLf & _
    "Email: " & email & vbCrLf & _
    "Local: " & arrEmail(0) & vbCrLf & _
    "Domain: " & arrEmail(1)
End Sub

 

Leave a Reply

Your email address will not be published. Required fields are marked *