We Recommend

Visual Basic 2008 Programmer's Reference Visual Basic 2008 Programmer's Reference
Visual Basic Orcas Programmer's Reference is a language tutorial and a reference guide to the upcoming Orcas release of Visual Basic. The tutorial provides basic material suitable for beginners but also includes in-depth content for more advanced developers.


Posted By

Saikou on 11/22/07


Tagged


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

sbbath


Name extractor


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.

  1. Private Sub ParseName(ByVal strInput, ByRef strFirstName, ByRef strLastName)
  2. Try
  3. ' Split the string into it's constituent words
  4. Dim strSplitName1() As String = strInput.Split(New Char() {" "})
  5.  
  6. ' Ensure any leading or trailing spaces are trimmed within each word
  7. Dim intIndex As Integer = 0
  8. For intIndex = 0 To strSplitName1.Length - 1
  9. strSplitName1(intIndex) = strSplitName1(intIndex).Trim
  10. Next
  11.  
  12. ' If only one word was found, treat it as a last name, with no first name
  13. If (strSplitName1.Length <= 1) Then
  14. strFirstName = ""
  15. strLastName = strSplitName1(0)
  16. Return
  17. End If
  18.  
  19. ' Check the first word for a title
  20. If ( _
  21. (String.Compare(strSplitName1(0), "Mr", True) = 0) OrElse _
  22. (String.Compare(strSplitName1(0), "Mr.", True) = 0) OrElse _
  23. (String.Compare(strSplitName1(0), "Mister", True) = 0) OrElse _
  24. (String.Compare(strSplitName1(0), "Mrs", True) = 0) OrElse _
  25. (String.Compare(strSplitName1(0), "Ms", True) = 0) OrElse _
  26. (String.Compare(strSplitName1(0), "Ms.", True) = 0) OrElse _
  27. (String.Compare(strSplitName1(0), "Miss", True) = 0) OrElse _
  28. (String.Compare(strSplitName1(0), "Dr", True) = 0) OrElse _
  29. (String.Compare(strSplitName1(0), "Dr.", True) = 0) OrElse _
  30. (String.Compare(strSplitName1(0), "Sir", True) = 0) OrElse _
  31. (String.Compare(strSplitName1(0), "Professor", True) = 0) OrElse _
  32. (String.Compare(strSplitName1(0), "Prof", True) = 0) OrElse _
  33. (String.Compare(strSplitName1(0), "Lord", True) = 0) OrElse _
  34. (String.Compare(strSplitName1(0), "Lady", True) = 0) OrElse _
  35. (String.Compare(strSplitName1(0), "Rev", True) = 0) OrElse _
  36. (String.Compare(strSplitName1(0), "Rev.", True) = 0) OrElse _
  37. (String.Compare(strSplitName1(0), "Reverend", True) = 0) OrElse _
  38. (String.Compare(strSplitName1(0), "Judge", True) = 0) OrElse _
  39. False _
  40. ) Then
  41.  
  42. ' A title was found, so the second and last words should be first and last name respectively
  43. If (strSplitName1.Length > 2) Then
  44. strFirstName = strSplitName1(1)
  45. strLastName = strSplitName1(strSplitName1.Length - 1)
  46. Else
  47. ' Only a title and one other word was found, so treat the other word as a last name
  48. strLastName = strSplitName1(1)
  49. End If
  50. ElseIf (strSplitName1(0).Length = 1) Then
  51. ' The first word is likely an initial, so use the first non-length-of-1 word as the last name
  52. strFirstName = strSplitName1(0)
  53. For Each strWord As String In strSplitName1
  54. If (strWord.Length > 1) Then
  55. strLastName = strWord
  56. End If
  57. Next
  58. ElseIf (strSplitName1(0).Contains(",")) Then
  59. ' Check the first word for a comma, if it contains one, it's likely to be 'Last, First' format
  60. strFirstName = strSplitName1(0).Replace(",", "").Trim()
  61. strLastName = strSplitName1(1)
  62. Else
  63. ' It's most likely to be a "First (Middle(s)) Last" format, so simply use the first and last words
  64. strFirstName = strSplitName1(0)
  65. strLastName = strSplitName1(strSplitName1.Length - 1)
  66. End If
  67. Catch ex As Exception
  68. 'EventLogger.LogException(ex)
  69. End Try
  70. End Sub

Report this snippet 

You need to login to post a comment.