string manipulation

dev1

New Member
Hi i would like to know if the following is possible:

define variable vc-add4 as character no-undo.
define variable vc-pcode as character no-undo.

assign vc-add4 = "Norfolk N15 7Bx"
vc-pcode = "N15 7bx".

I would like to search for the post code (vc-pcode) in the address line (vc-add4), and if it exists strip it out. I can achieve this using the index function and substring.

My problem is this, sometimes the postcode in the address line will contain two or more spaces ie N15 7Bx, whilst the postcode only contains one space.

Is there a way to identify and strip out the postcode from the address line in this case?

Winnt 4.0 9.1d.
 
You could first of all ensure that neither of your strings contains double spaces.

Then use REPLACE to strip out matching postcodes.

If you want more clarification let me know
 
I haven't tested this code but I think it will do what you want:

Code:
DEFINE VARIABLE strToTidy AS CHARACTER NO-UNDO.
 
ASSIGN
   strToTidy = YourAddressLine.
 
REPEAT WHILE SUBSTRING(strToTidy,"  ") <> 0:
  ASSIGN
   strToTidy = REPLACE(strToTidy,"  "," ") /* Replace double space with single */
END.
 
REPLACE(strToTidy,YourPostCode,"").
 
ASSIGN
  YourAddressLine = strToTidy.
 
Crittar,

That hit the spot! Seems to sort out my probs.
However, i just used the replace function, without looping, replace does this for me.



Crittar said:
I haven't tested this code but I think it will do what you want:

Code:
DEFINE VARIABLE strToTidy AS CHARACTER NO-UNDO.
 
ASSIGN
strToTidy = YourAddressLine.
 
REPEAT WHILE SUBSTRING(strToTidy," ") <> 0:
ASSIGN
strToTidy = REPLACE(strToTidy," "," ") /* Replace double space with single */
END.
 
REPLACE(strToTidy,YourPostCode,"").
 
ASSIGN
YourAddressLine = strToTidy.
 
Dev1,

Glad it helped.

The only reason for the loop is in case there are more than two spaces anywhere in the string to tidy.

You could for example pass the string:

"This.............string............has.a......lot of...............spaces"
(where the dots represent spaces)

and the returnd string would be:

"This string has a lot of spaces".

The "tidy" routine could easily be made into a function if it's something you might use frequently.
 
Back
Top