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!

Substring problem 2

Status
Not open for further replies.

SuperBob

Technical User
Sep 18, 2001
23
GB
I have a need to process a postcode value passed to a form. The postcode should be in two parts, the first being the "region" and the second the precise location, e.g. B97 5UX. I am checking that a space exists, in order to identify where the first part of the postcode ends. My problem however is to separate out the alphabetic first 1-2 characters, and then the 1-2 numerics that follow, as these two parts are used to locate a match in another table.

The first part of the postcode could be (if legitimate) between 2 and 4 characters, and I could use tedious coding to do this job, but am looking for something more elegant.

Any ideas?
 
Ok SuperBob, I am really going for elegant here. Cut-N-Paste the code below into a prg file and run it from within VFP.

cPostal = "B97 5UX"

cFirstPart = GETWORDNUM(cPostal,1, ' ')
cFirstAlpha = CHRTRAN(cFirstPart, '0123456789', '')
*!* cFirstNum = CHRTRAN(cFirstPart, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', '')
cFirstNum = CHRTRAN(cFirstPart, cFirstAlpha, '')

cSecondPart = GETWORDNUM(cPostal,2, ' ')
cSecondAlpha = CHRTRAN(cSecondPart, '0123456789', '')
*!* cSecondNum = CHRTRAN(cFirstPart, cSecondAlpha, '')
cSecondNum = CHRTRAN(cSecondPart, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', '')

MESSAGEBOX("Region:" + CHR(9) + cFirstAlpha + CHR(13) ;
+ CHR(9) + cFirstNum + CHR(13) + CHR(13) ;
+ "Location:" + CHR(9) + cSecondAlpha + CHR(13) ;
+ CHR(9) + cSecondNum)

Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
Thanks for the (very rapid) help. Unfortunately, I don't have VFP7 so I can's use Getwordnum, but I can find the first "word" using AT and the rest of the code will then enable me to isolate alpha and numeric parts of the postcode.

Thanks again!
 
I am sorry that I assumed VFP 7 or greater...allow me to make a backwards compatible suggestion:

SET LIBRARY TO foxtools.fll
*!* wordnum(expC1, expN, [expC2]) <-Foxtools function

cPostal = &quot;B97 5UX&quot;

cFirstPart = wordnum(cPostal,1, ' ')
cFirstAlpha = CHRTRAN(cFirstPart, '0123456789', '')
*!* cFirstNum = CHRTRAN(cFirstPart, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', '')
cFirstNum = CHRTRAN(cFirstPart, cFirstAlpha, '')

cSecondPart = wordnum(cPostal,2, ' ')
cSecondAlpha = CHRTRAN(cSecondPart, '0123456789', '')
*!* cSecondNum = CHRTRAN(cFirstPart, cSecondAlpha, '')
cSecondNum = CHRTRAN(cSecondPart, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', '')

MESSAGEBOX(&quot;Region:&quot; + CHR(9) + cFirstAlpha + CHR(13) ;
+ CHR(9) + cFirstNum + CHR(13) + CHR(13) ;
+ &quot;Location:&quot; + CHR(9) + cSecondAlpha + CHR(13) ;
+ CHR(9) + cSecondNum)

Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
lcData = 'B97 5UX'
lnSpace = ATC(&quot; &quot;,lcData)
lnPartOne = SUBSTR(lcData,1,lnSpace-1)
lnPartTwo = SUBSTR(lcData,lnSpace+1)
? lnPartOne
? lnPartTwo

Jim Osieczonek
Delta Business Group, LLC
 
Thanks all - I've got things working.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top