I need help parsing a string and placing it into 3 separate variables

Posted on 2014-04-16
Medium Priority
Last Modified: 2014-05-11
Hi Experts,
I need help parsing a string in my VB6 application.  I have a list of names formatted in the following manner:  

Jones, Martha
Smith, Olivia R

I want to parse  the name so that the LAST NAME, FIRST NAME, and Middle Initial are placed in the following 3 variables:  strLastName, strFirstName, strMiddleInit (if it is there).  How can I do this?

Thanks in advance,
Question by:mainrotor
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 46

Accepted Solution

aikimark earned 668 total points
ID: 40005858
This should come close
Dim oRE as object
Dim oMatches as object
Dim oM as object      'only needed if passing a lot of names at once
Dim strWholeName as string
Dim strLastName as string, strFirstName as string, strMiddleInit as string

set oRE = createobject("vbscript.regexp")
oRE.Global = True
oRE.pattern = "(.*?), ([^ ]*) ?(.*)"
strWholename = "Jones, Martha"
if oRE.test(strWholename) THen
   set oMatches = oRE.Execute(strWholename)
   strLastName = oMatches(0).submatches(0)
   strFirstName = oMatches(0).submatches(1)
   strMiddleInit = oMatches(0).submatches(2)
End If

strWholename = "Smith, Olivia R"
if oRE.test(strWholename) THen
   set oMatches = oRE.Execute(strWholename)
   strLastName = oMatches(0).submatches(0)
   strFirstName = oMatches(0).submatches(1)
   strMiddleInit = oMatches(0).submatches(2)
End If

Open in new window

LVL 76

Assisted Solution

GrahamSkan earned 668 total points
ID: 40006099
Here is a way with a bit less code:
Dim strParts() As String
    Dim strSurname  As String
    Dim strFirstName  As String
    Dim strMiddle  As String
    strParts = Split(Replace(strFullName, ",", ""), " ")
    strSurname = strParts(0)
    strFirstName = strParts(1)
    If UBound(strParts) > 1 Then
        strMiddle = strParts(2)
    End If

Open in new window

LVL 16

Assisted Solution

HooKooDooKu earned 664 total points
ID: 40006639
Here's some strait forward code doing simple InStr searches and basic Left, Mid, and Len functions:
Dim I as Integer
I = InStr( strFullName "," )
If I > 0 Then
    strLastName = Left$( strFullName, I - 1 )  'Save Last Name    

    strFullName = Trim$(Mid$( strFullName, I + 1 ))  'Update Name to just 1st and MI

    I = InStrRev( strFullName, " " )
    If I > 0 Then
        strMiddleInit = Mid$( strFullName, I + 1 )

        'Check for double 1st names (i.e. name with a space, yet no middle initial
        If Len( strMiddleInit ) > 1 Then
            strMiddleInit = ""
            strFirstName = strFullName 
            strFirstName = Left$( strFirstName, Len( strFullName ) - 2 )
        End If
        'No Middle Initial
        strFirstName = strFullName
        strMiddleInit = ""
    End If
End If

Open in new window

But the
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 46

Expert Comment

ID: 40006939
comment on http:#a40006099 - Graham's code
* if you have a double last name (not hyphenated), the parse will not be correct.  Think of Spanish family names.
* a correct version of Graham's code would not use the Replace() function, instead splitting on the ", " delimiter first and any remaining space delimiter second.
    strParts = Split(strFullName, ", ")
    strSurname = strParts(0)
    strParts = Split(Trim(strParts(1)), " ")
    strFirstName = strParts(0)
    If UBound(strParts) > 0 Then
        strMiddle = strParts(1)
    End If

Open in new window

comment on http:#a40006639 - HooKooDooKu's code
* if someone has a double first name, would we expect them to also have a middle initial?
* I do like your check for a single character middle initial.  It has caused me to rethink my regex pattern.  This pattern should handle multiple first names with a middle initial
^(.*?), \b(.*?)\b(.{0,1}?)$

Open in new window

It parses "briggs, joe bob d" into
firstname=joe bob

It parses "briggs, joe bob" into
firstname=joe bob
LVL 93

Expert Comment

by:Patrick Matthews
ID: 40008060
If you need something more robust, you might give this a spin:


It's not perfect, of course, and some edge cases will cause it to return unexpected results.
LVL 49

Expert Comment

by:Martin Liss
ID: 40057280
I've requested that this question be closed as follows:

Accepted answer: 168 points for aikimark's comment #a40005858
Assisted answer: 166 points for GrahamSkan's comment #a40006099
Assisted answer: 166 points for matthewspatrick's comment #a40008060

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question