EmailParts.vbs

'=========================================================================
' EmailParts.vbs
' VERSION: 1.0
' AUTHOR: Brian Steinmeyer
' EMAIL: [email protected]
' WEB: https://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("[email protected]")
Call EmailParts2("[email protected]")
Call EmailParts3("[email protected]")
Call EmailParts4("[email protected]")

'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