Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: VB7
Expertise: beginner
Dec 1, 2003



Building the Right Environment to Support AI, Machine Learning and Deep Learning

ExtractFirstAndLastName - Extracting the first/last name from a string will the full name

' Extract the first and last name from a string will the full name.
' The routine handles the "LastName, FirstName" and "FirstName LastName" 
' formats, and returns the first and last name with byref output parameters.
' Example:
'    Dim firstName, lastName As String
'    ExtractFirstAndLastName("Marco", firstName, lastName)
'    Debug.WriteLine("FirstName=" & firstName & " " & "LastName=" & lastName)
'    ExtractFirstAndLastName("Marco Bellinaso", firstName, lastName)
'    Debug.WriteLine("FirstName=" & firstName & " " & "LastName=" & lastName)
'    ExtractFirstAndLastName("Bellinaso, Marco", firstName, lastName)
'    Debug.WriteLine("FirstName=" & firstName & " " & "LastName=" & lastName)

Sub ExtractFirstAndLastName(ByVal fullName As String, ByRef firstName As String, _
    ByRef lastName As String)

    firstName = ""
    lastName = ""

    ' if the fullname has a comma, it considers the 
    ' "LastName, FirstName" format, and 
    ' "FirstName LastName" otherwise
    Dim commaPos As Integer = fullName.IndexOf(",")
    If commaPos > -1 Then
        lastName = fullName.Substring(0, commaPos).Trim()
        firstName = fullName.Substring(commaPos + 1).Trim()
        ' find the space
        Dim spacePos As Integer = fullName.IndexOf(" ")
        ' if the space is not found, consider 
        ' everything as first name, otherwise do the split
        If spacePos = -1 Then
            firstName = fullName
            firstName = fullName.Substring(0, spacePos).Trim()
            lastName = fullName.Substring(spacePos).Trim()
        End If
    End If
End Sub
Marco Bellinaso
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date