CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2013
    Posts
    1

    Split Name function

    Can anyone help see where I went wrong in the function below?
    Code:
    Function ParseOutNames(FullName As String) As Variant
    
    Dim FirstName As String
    Dim LastName As String
    Dim MidInitial As String
    Dim Suffix As String
    Dim Pos As Integer
    Dim Pos2 As Integer
    Dim Pos3 As Integer
    
    Pos = InStr(1, FullName, ",", vbTextCompare)
    If Pos = 0 Then
        Pos = Len(FullName) + 1
    End If
    LastName = Trim(Left(FullName, Pos - 1))
    
    Pos2 = InStr(1, LastName, " ", vbTextCompare)
    If Pos2 Then
        Pos3 = InStr(Pos2 + 1, LastName, " ", vbTextCompare)
        If Pos3 Then
            Suffix = Right(LastName, Len(LastName) - Pos3)
            LastName = Left(LastName, Pos3 - 1)
        Else
            Suffix = Right(LastName, Len(LastName) - Pos2)
            LastName = Left(LastName, Pos2 - 1)
        End If
    End If
    
    Pos2 = InStr(Pos + 2, FullName, " ", vbTextCompare)
    If Pos2 = 0 Then
        Pos2 = Len(FullName)
    End If
    
    If Pos2 > Pos Then
        FirstName = Mid(FullName, Pos + 1, Pos2 - Pos)
        MidInitial = Right(FullName, Len(FullName) - Pos2)
    End If
    
    Pos = InStr(1, LastName, "-", vbTextCompare)
    If Pos Then
        LastName = Trim(StrConv(Left(LastName, Pos), vbProperCase)) & _
        Trim(StrConv(Right(LastName, Len(LastName) - Pos), vbProperCase))
    Else
        LastName = Trim(StrConv(LastName, vbProperCase))
    End If
    
    FirstName = Trim(StrConv(FirstName, vbProperCase))
    MidInitial = Trim(StrConv(MidInitial, vbProperCase))
    Suffix = Trim(StrConv(Suffix, vbProperCase))
    '
    ' suffix handling
    '
    Select Case UCase(Suffix)
        Case "JR", "SR", "II", "III", "IV", "MD", "PHD", "PH.D", "M.D."
    
        Case Else
            If Not IsNumeric(Left(Suffix, 1)) Then
                LastName = LastName & " " & Suffix
                Suffix = ""
            End If
    End Select
    
    ParseOutNames = Array(LastName, FirstName, MidInitial, Suffix)
    
    End Function
    Last edited by WizBang; January 6th, 2013 at 02:08 PM. Reason: Added [code] tags

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Split Name function

    Please use CODE Tags to post code.

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Split Name function

    Hard to follow that code but I would suggest a different approach.
    All you really seem to need is a call to the Split() function

    Code:
    Dim NameParts() as String
    
    NameParts=Split(FullName," ")
    This will give you an array with the parts of the name string split on spaces you can then check the number of elements and the values of each with less code that will be easier to read.
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Dec 2012
    Posts
    38

    Re: Split Name function

    I should use a structure instead of an array.

    However if you don't show an example of how your string is made, and what parts you're interested in, it's hard to give a careful answer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured