?
Solved

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

Posted on 2014-04-16
8
Medium Priority
?
234 Views
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:  

LAST NAME, FIRST NAME Middle Initial
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,
mrotor
0
Comment
Question by:mainrotor
6 Comments
 
LVL 46

Accepted Solution

by:
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

0
 
LVL 76

Assisted Solution

by:GrahamSkan
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

0
 
LVL 16

Assisted Solution

by:HooKooDooKu
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 
        Else
            strFirstName = Left$( strFirstName, Len( strFullName ) - 2 )
        End If
    Else
        'No Middle Initial
        strFirstName = strFullName
        strMiddleInit = ""
    End If
End If

Open in new window


But the
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 46

Expert Comment

by:aikimark
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
Lastname=briggs
firstname=joe bob
MI=d

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

Expert Comment

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

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/A_1819-Parsing-Names-in-MS-Office-Visual-Basic-6-and-Visual-Basic-for-Applications.html

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

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.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

864 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