separate Firstname and lastname

i am looking for a function to parse name these pattern. I need firstname and lastname


Jenny Smith
Joe and Jim Smith
Sam Joe & Leslie Williams
Biden, Joe
James John III
Tim & Jim
Smith, Luke and Jim
VBdotnet2005Asked:
Who is Participating?
 
ZtinelConnect With a Mentor Commented:
TRY THIS:
    Sub ParseName(ByVal name As String)

        Dim arr As String()
        Dim arrcount As Integer
        arr = name.Split(" ")
        arrcount = arr.Count
        For i As Integer = 0 To arr.Length - 1

        Next
        If name.Contains(",") Then
            Label2.Text = name.Substring(0, name.IndexOf(","))
            Label1.Text = name.Substring(name.IndexOf(",") + 2)
        ElseIf arr(arrcount - 2) = "&" Or _
        arr(arrcount - 2) = "AND".ToUpper Then
            Label1.Text = name
        ElseIf arr(arrcount - 1) = "I".ToUpper Or _
                arr(arrcount - 1) = "II".ToUpper Or _
                arr(arrcount - 1) = "III".ToUpper Or _
                arr(arrcount - 1) = "IV".ToUpper Or _
                arr(arrcount - 1) = "V".ToUpper Or _
                arr(arrcount - 1) = "VI".ToUpper Or _
                arr(arrcount - 1) = "JR".ToUpper Or _
                arr(arrcount - 1) = "JR.".ToUpper Then
            Label2.Text = arr(arrcount - 2) & " " & arr(arrcount - 1)
            For i As Integer = 0 To arrcount - 3
                Label1.Text = Label1.Text & " " & arr(i)
            Next
        Else
            Label2.Text = arr(arrcount - 1)
            For i As Integer = 0 To arrcount - 2
                Label1.Text = Label1.Text & " " & arr(i)
            Next
        End If

    End Sub
0
 
cyberkiwiCommented:
You can start by showing us what the result should be for each.

e.g. Tim & Jim => ?
0
 
VBdotnet2005Author Commented:
Firstname.       Lastname
jenny.              Smith
joe&jim.           Smith
Sam & leslie.    Williams
joe.                   Binden
James.              John iii
tim&jim.           ''
Luke & jim.     Smith
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
VBdotnet2005Author Commented:
I hope my sample helps.
0
 
VBdotnet2005Author Commented:
could you add this pattern also?
Jim & john Williams

result would be

firstname     lastname
jim & john      williams
0
 
VBdotnet2005Author Commented:
if try

jim wiliams jr.

it returns

jr as lastname
Jim as firstname
0
 
VBdotnet2005Author Commented:
Ztinel,

it returns as
jr as lastname
jim williams as firstname
0
 
Carlos VillegasConnect With a Mentor Full Stack .NET DeveloperCommented:
Hello, try this:
    Sub MyParseName(ByVal fullName As String)
        Dim match As System.Text.RegularExpressions.Match =
            System.Text.RegularExpressions.Regex.Match(fullName, "^(?:(?<FirstName>(?:[a-zA-Z]+\s?)+(?:\s(?:and|&)\s(?:[a-zA-Z]+)))(?<LastName>\s[a-zA-Z]+(?:\s(?:I|II|III|IV|V|VI))?)?)$|^(?<FirstName>(?:[a-zA-Z]+))(?<LastName>\s[a-zA-Z]+(?:\s(?:I|II|III|IV|V|VI))?)$|^(?:(?<LastName>[a-zA-Z]+(?:\s(?:I|II|III|IV|V|VI))?)(?:\,\s))(?<FirstName>(?:[a-zA-Z]+)+(?:\s(?:and|&)\s(?:[a-zA-Z]+))?)$")

        Dim firstName As String, lastName As String
        If (match.Success) Then
            firstName = match.Groups("FirstName").Value
            lastName = match.Groups("LastName").Value

            MsgBox("FirstName: " & firstName & vbCrLf & "LastName: " & lastName, MsgBoxStyle.Information)
        Else
            MsgBox("Sorry I don't known how to parse this name.", MsgBoxStyle.Information)
        End If
    End Sub

Open in new window


I'm sure than it can be done in a better way using regular expressions, but it requires more work, I hope this code will be useful to you.

0
 
VBdotnet2005Author Commented:
yv989c,
Does it parse all of this? I have not try it.

Jenny Smith
Joe and Jim Smith
Sam Joe & Leslie Williams
Biden, Joe
James John III
Tim & Jim
Smith, Luke and Jim
jim wiliams jr.
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hello! I did some improvements to my code:
Sub MyParseName(ByVal fullName As String)
	Dim match As System.Text.RegularExpressions.Match =
	    System.Text.RegularExpressions.Regex.Match(fullName, "^(?:(?<FirstName>(?:[A-Z]+\s?)+(?:\s(?:and|&)\s(?:[A-Z]+)))(?<LastName>\s[A-Z]+(?:\s(?:I|II|III|IV|V|VI|JR\.?))?)?)$|^(?<FirstName>(?:[A-Z]+))(?<LastName>\s[A-Z]+(?:\s(?:I|II|III|IV|V|VI|JR\.?))?)$|^(?:(?<LastName>[A-Z]+(?:\s(?:I|II|III|IV|V|VI|JR\.?))?)(?:\,\s))(?<FirstName>(?:[A-Z]+)+(?:\s(?:and|&)\s(?:[A-Z]+))?)$", System.Text.RegularExpressions.RegexOptions.IgnoreCase)

	Dim firstName As String, lastName As String
	If (match.Success) Then
	    firstName = match.Groups("FirstName").Value
	    lastName = match.Groups("LastName").Value

	    MsgBox("FirstName: " & firstName & vbCrLf & "LastName: " & lastName, MsgBoxStyle.Information)
	Else
	    MsgBox("Sorry I don't known how to parse this name.", MsgBoxStyle.Information)
	End If
End Sub

Open in new window


Try it!
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
YES, try it! :)
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
My last code of course... added support for Jr. and IgnoreCase
0
 
ZtinelCommented:
hi vbdotnet, I tried Jim & john Williams and jim wiliams jr. and my code worked fine.
You can test my code by adding textbox (name imput), button that calls the procedure ParseName with input parameter textbox.text and two labels that will hold first and last names. But be sure to clear the labels upon loading.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.