Published in: Visual Basic
Attempts to extract a first name and last name from a string, such as "Mr Smith", "Smith, John", "John James Smith", "J Smith", "Smith, J" etc.
Private Sub ParseName(ByVal strInput, ByRef strFirstName, ByRef strLastName) Try ' Split the string into it's constituent words Dim strSplitName1() As String = strInput.Split(New Char() {" "}) ' Ensure any leading or trailing spaces are trimmed within each word Dim intIndex As Integer = 0 For intIndex = 0 To strSplitName1.Length - 1 strSplitName1(intIndex) = strSplitName1(intIndex).Trim Next ' If only one word was found, treat it as a last name, with no first name If (strSplitName1.Length <= 1) Then strFirstName = "" strLastName = strSplitName1(0) Return End If ' Check the first word for a title If ( _ (String.Compare(strSplitName1(0), "Mr", True) = 0) OrElse _ (String.Compare(strSplitName1(0), "Mr.", True) = 0) OrElse _ (String.Compare(strSplitName1(0), "Mister", True) = 0) OrElse _ (String.Compare(strSplitName1(0), "Mrs", True) = 0) OrElse _ (String.Compare(strSplitName1(0), "Ms", True) = 0) OrElse _ (String.Compare(strSplitName1(0), "Ms.", True) = 0) OrElse _ (String.Compare(strSplitName1(0), "Miss", True) = 0) OrElse _ (String.Compare(strSplitName1(0), "Dr", True) = 0) OrElse _ (String.Compare(strSplitName1(0), "Dr.", True) = 0) OrElse _ (String.Compare(strSplitName1(0), "Sir", True) = 0) OrElse _ (String.Compare(strSplitName1(0), "Professor", True) = 0) OrElse _ (String.Compare(strSplitName1(0), "Prof", True) = 0) OrElse _ (String.Compare(strSplitName1(0), "Lord", True) = 0) OrElse _ (String.Compare(strSplitName1(0), "Lady", True) = 0) OrElse _ (String.Compare(strSplitName1(0), "Rev", True) = 0) OrElse _ (String.Compare(strSplitName1(0), "Rev.", True) = 0) OrElse _ (String.Compare(strSplitName1(0), "Reverend", True) = 0) OrElse _ (String.Compare(strSplitName1(0), "Judge", True) = 0) OrElse _ False _ ) Then ' A title was found, so the second and last words should be first and last name respectively If (strSplitName1.Length > 2) Then strFirstName = strSplitName1(1) strLastName = strSplitName1(strSplitName1.Length - 1) Else ' Only a title and one other word was found, so treat the other word as a last name strLastName = strSplitName1(1) End If ElseIf (strSplitName1(0).Length = 1) Then ' The first word is likely an initial, so use the first non-length-of-1 word as the last name strFirstName = strSplitName1(0) For Each strWord As String In strSplitName1 If (strWord.Length > 1) Then strLastName = strWord End If Next ElseIf (strSplitName1(0).Contains(",")) Then ' Check the first word for a comma, if it contains one, it's likely to be 'Last, First' format strFirstName = strSplitName1(0).Replace(",", "").Trim() strLastName = strSplitName1(1) Else ' It's most likely to be a "First (Middle(s)) Last" format, so simply use the first and last words strFirstName = strSplitName1(0) strLastName = strSplitName1(strSplitName1.Length - 1) End If Catch ex As Exception 'EventLogger.LogException(ex) End Try End Sub
You need to login to post a comment.
