Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help Separating Full Names into Parts!

Status
Not open for further replies.

prestorush

IS-IT--Management
May 7, 2003
6
US
If i have a name in the format "John B Smith" contained inside a single field how can I separate the name into "John" for a first name field "B" for a middle initial field and "Smith" for a last name field. I can pull the first name using some basic fox syntax but I'm stumped on how to get the initial and last name out! THANKS!
 
See thread184-567614. If you want the code I've sent to others, just leave an email address (feel free to obscure it - e.g. yourname at yourdomain dot com.)

Rick
 
Have a try with this:


cfield= ' John B Smith'
cfld = alltrim(cfield)
lspace=at(' ',cfld)
firstname = left(cfld,lspace-1)
rspace=rat(' ',cfld)
lastname = right(cfld,len(cfld)-rspace)
lenmid = rspace-lspace
midname=substr(cfld,lspace+1,lenmid)
? firstname
? midname
? lastname

Rob.
 
I would like the the code samples please!
Thanks a million.
johnb@servantpc.com
 
Here's an uncompleted class that will parse name strings.

I looked at a VB application I wrote a while ago that had
to perform something similiar, and converted the concepts
to VFP.

It's sparsely commented at this point, but should work as
the basis for a more robust class.

Darrell

[tt]


CLEAR

* Test class *
LOCAL oNameOps
oNameOps = CREATEOBJECT("C_NameOps")
oNameOps.Parse("Dr. John, B. Smith, Phd.,M.D.")


? "Honorific ",oNameOps.cHonorific
? "First Name ",oNameOps.cFirstName
? "Middle Name",oNameOps.cMiddleName
? "Last Name ",oNameOps.cLastName
? "Suffix ",oNameOps.cSuffix
?
? "Full Name ",oNameOps.GetFullName()

?

oNameOps.Parse("Mary Jones")

? "Honorific ",oNameOps.cHonorific
? "First Name ",oNameOps.cFirstName
? "Middle Name",oNameOps.cMiddleName
? "Last Name ",oNameOps.cLastName
? "Suffix ",oNameOps.cSuffix
?
? "Full Name ",oNameOps.GetFullName()


* Insure protected assign methods of class are functioning properly

oNameOps.cFirstName = 10
? oNameOps.cFirstName


************************************************************
*-- Class: C_NameOps
*-- Author: Darrell C. Greenhouse
*-- Last Upate:
*-- Created: 2003.08.06
*-- Description: Class to parse name strings into their
* subsequent parts
*-- Pass:
*-- Returns:
*-- Collaborates:
*-- Assumuptions:
*-- Notes: This class currently will fail for names
*-- with more than 6 parts
*--
*-- Todo: Complete the parse method for name parts
*-- equal to 3 and 4. Also, conditionals in
*-- the parse method need to be fleshed out a
*-- little more.
*-- Revisions:
************************************************************
DEFINE CLASS C_NameOps AS CUSTOM
#DEFINE HONORIFICS "Mr|Mrs|Miss|Ms|Dr|Sr|Fr" && Add other honorifics here
#DEFINE SUFFIXES "Phd|Md|Js|Phd.,Md|Phd.,M.D|Br"
#DEFINE NonKosher ","

PROTECTED ARRAY aNameParts[1]

nNameParts = 0 && Count of name parts. i.e. Honorific(Mr. Ms. Dr. etc.), First Name, Middle, Last, Suffix

cHonorific = ""
cFirstName = ""
cMiddleName = ""
cLastName = ""
cSuffix = ""

*** Public Methods ***

PROCEDURE Parse(lcNameIn)
LOCAL lbHasHonorific, lbHasSuffix && Flags whether name has a honorific and/or a suffix

WITH THIS
* Clear name parts
.cHonorific = ""
.cFirstName = ""
.cMiddleName = ""
.cLastName = ""
.cSuffix = ""

.nNameParts = ALINES(.aNameParts,STRTRAN(lcNameIn," ",CHR(13)))

DO CASE
CASE .nNameParts == 1 && Assume First Name only
.cFirstName = .aNameParts[1]

CASE .nNameParts == 2 && Assume Honorific and Last Name; or First and Last Name
IF .IsHonorific(.aNameParts[1]) && Is the first name part a honorific
.cHonorific = .aNameParts[1]
ELSE
.cFirstName = .aNameParts[1]
ENDIF
.cLastName = .aNameParts[2]

CASE .nNameParts == 3 && Assumme Honorific, First Name, and Last Name or
* First Name, Middle, and Last Name or
* Honorific, Last Name, and Suffix

CASE .nNameParts == 4 &&

CASE .nNameParts == 5 && Assume all name parts are contained
.cHonorific = .aNameParts[1]
.cFirstName = .aNameParts[2]
.cMiddleName = .aNameParts[3]
.cLastName = .aNameParts[4]
.cSuffix = .aNameParts[5]
ENDCASE
ENDWITH

ENDPROC

FUNCTION GetFullName
LOCAL i, lcFullName
lcFullName = ""
FOR i = 1 TO THIS.nNameParts
lcFullName = lcFullName + THIS.aNameParts[ i ]+IIF(i<THIS.nNameParts,&quot; &quot;,&quot;&quot;)
NEXT
RETURN lcFullName
ENDFUNC

*** End of - Public Methods ***


*** Protected Methods ***

PROTECTED FUNCTION IsInternal && Is the caller of one of the methods an internal method?
LOCAL lcCaller, i, lnGetSpace
i = 1
DO WHILE !EMPTY(SYS(16,i))
lcCaller = SYS(16,i)
i = i + 1
ENDDO
lcCaller = SYS(16,i-3)
lnGetSpace = AT(&quot; &quot;,lcCaller)
IF lnGetSpace <> 0
lcCaller = SUBSTR(lcCaller,lnGetSpace+1)
ENDIF
lnGetSpace = AT(&quot; &quot;,lcCaller)
IF lnGetSpace <> 0
lcCaller = LEFT(lcCaller,lnGetSpace-1)
ENDIF
lcCaller = JUSTSTEM(lcCaller)
RETURN lcCaller == UPPER(THIS.NAME)
ENDFUNC


*** Access Methods ***

PROTECTED FUNCTION cFirstName_access
LOCAL lcReturnVal
lcReturnVal = THIS.cFirstName
IF !THIS.IsInternal() .AND. RIGHT(lcReturnVal,1) $ NonKosher
lcReturnVal = LEFT(lcReturnVal,LEN(lcReturnVal)-1)
ENDIF
RETURN lcReturnVal
ENDFUNC

PROTECTED FUNCTION cMiddleName_access
LOCAL lcReturnVal
lcReturnVal = THIS.cMiddleName
IF !THIS.IsInternal() .AND. RIGHT(lcReturnVal,1) $ NonKosher
lcReturnVal = LEFT(lcReturnVal,LEN(lcReturnVal)-1)
ENDIF
RETURN lcReturnVal
ENDFUNC

PROTECTED FUNCTION cLastName_access
LOCAL lcReturnVal
lcReturnVal = THIS.cLastName
IF !THIS.IsInternal() .AND. RIGHT(lcReturnVal,1) $ NonKosher
lcReturnVal = LEFT(lcReturnVal,LEN(lcReturnVal)-1)
ENDIF
RETURN lcReturnVal
ENDFUNC

*** End of - Access Methods ***

*** Assign Methods ***
* Only allow internal methods to modify these values.
PROTECTED PROCEDURE cHonorific_assign(lvNewValue)
IF THIS.IsInternal()
THIS.cHonorific = lvNewValue
ENDIF
ENDPROC
PROTECTED PROCEDURE cFirstName_assign(lvNewValue)
IF THIS.IsInternal()
THIS.cFirstName = lvNewValue
ENDIF
ENDPROC
PROTECTED PROCEDURE cMiddleName_assign(lvNewValue)
IF THIS.IsInternal()
THIS.cMiddleName = lvNewValue
ENDIF
ENDPROC
PROTECTED PROCEDURE cLastName_assign(lvNewValue)
IF THIS.IsInternal()
THIS.cLastName = lvNewValue
ENDIF
ENDPROC
PROTECTED PROCEDURE cSuffix_assign(lvNewValue)
IF THIS.IsInternal()
THIS.cSuffix = lvNewValue
ENDIF
ENDPROC
PROTECTED PROCEDURE nNameParts_assign(lvNewValue)
IF THIS.IsInternal()
THIS.nNameParts = lvNewValue
ENDIF
ENDPROC
*** End of - Assign Methods ***


* Is a suspect name part a honorific?
PROTECTED FUNCTION IsHonorific(lcSuspect)
LOCAL ARRAY laHonorifics[1]
LOCAL lnHonorificCount, i, lbIsAHonorific
lnHonorificCount = ALINES(laHonorifics,STRTRAN(HONORIFICS,&quot;|&quot;,CHR(13)))

* Strip period from end of suspect if it exists
lcSuspect = IIF(RIGHT(lcSuspect,1)==&quot;.&quot;,LEFT(lcSuspect,LEN(lcSuspect)-1),lcSuspect)

FOR i = 1 TO lnHonorificCount
IF UPPER(lcSuspect) == UPPER(laHonorifics[ i ])
lbIsAHonorific = .T.
EXIT
ENDIF
NEXT

RETURN lbIsAHonorific
ENDFUNC


* Is a suspect name part a suffix?
PROTECTED FUNCTION IsSuffix(lcSuspect)
LOCAL ARRAY laSuffixes[1]
LOCAL lnSuffixCount, i, lbIsASuffix

lnSuffixCount = ALINES(laSuffixes,STRTRAN(SUFFIXES,&quot;|&quot;,CHR(13)))

* Strip period from end of suspect if it exists
lcSuspect = IIF(RIGHT(lcSuspect,1)==&quot;.&quot;,LEFT(lcSuspect,LEN(lcSuspect)-1),lcSuspect)

FOR i = 1 TO lnSuffixCount
IF UPPER(lcSuspect) == UPPER(laSuffixes[ i ])
lbIsASuffix = .T.
EXIT
ENDIF
NEXT

RETURN lbIsASuffix
ENDFUNC

*** End of - Protected Methods ***

ENDDEFINE
[/tt]

'We all must do the hard bits so when we get bit we know where to bite' :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top