Link to home
Start Free TrialLog in
Avatar of mainrotor
mainrotor

asked on

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

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
ASKER CERTIFIED SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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=
If you need something more robust, you might give this a spin:

https://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.
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.