?
Solved

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

Posted on 2014-04-16
8
Medium Priority
?
233 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
[X]
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
8 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

649 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