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!

find and replace 2 or more spaces 7

Status
Not open for further replies.

borsker

Programmer
Jul 24, 2002
147
US
I have a field that sometimes has multiple blanks in the data. for example "myfirst mylast" and it needs to be "myfirst mylast". i am using this command but i do not know how to look for more than one character at a time. is this even possible?

REPLACE ALL cField WITH CHRTRAN(cField,CHR(32),'')

Thanks you for any answers.
 
Hi Borsker,
To replace 2 spaces with 1 space use this:

REPLACE ALL cField WITH CHRTRAN(cField, space(2), space(1))

I think this is what you want,
Shardlow.
 
Sorry Borsker,
I used the wrong function. Try this:

REPLACE ALL cField WITH STRTRAN(cField, space(2), space(1))


Goodluck,
Shardlow.
 
Shardlow

STRTRAN() won't remove all multiple spaces. If there are six spaces, only three are removed, leaving three spaces, still too many.

borsker

Only way I know remove all extraneous spaces is to use a loop like this:

[tt]
a = "" && Empty string
DO WHILE LEN(a) <> LEN(cField)
a = cField
cField = STRTRAN(cField, space(2), space(1))
ENDDO
[/tt]

This was done off the top of my head and is UNTESTED. However, once debugged, a loop like this should solve the problem.


mmerlinn

"Political correctness is the BADGE of a COWARD!"

 
Code:
local llchanged
use or select the table cField is in...
Scan
  llchanged = .f.
  m.cField = cField
  Do while at("  ",m.cField)>0
    llchanged = .t.
    m.cField = STRTRAN(m.cField, "  ", " ")
  Enddo 
  if llchanged  && only replace when something changed
    replace cField with m.cField
  endif 
Endscan

Rob.
 
By the way mmerlinn,

this code:
Code:
a = ""    &&  Empty string
DO WHILE LEN(a) <> LEN(cField)
    a = cField
    cField = STRTRAN(cField, space(2), space(1))
ENDDO

runs forever if cField isn't reduced to an empty string...

Rob.

 
mmerlinn,

sorry, I did miss the a = cField statement, so your solution is correct.

Rob.
 

rob444

Tinkered with your solution a bit and came up with this, again UNTESTED:

[tt]
USE or SELECT the table cField is in...
SCAN
m.cField = cField
DO WHILE AT(" ", m.cField) > 0
m.cField = STRTRAN(m.cField, space(2), space(1))
ENDDO
IF cField <> m.cField && Only replace when something changed
REPLACE cField WITH m.cField
ENDIF
ENDSCAN
[/tt]

I don't remember off of the top of my head whether the replacement test (IF cField <> m.cField) is correct or if it should be IF m.cField <> cField.


mmerlinn

"Political correctness is the BADGE of a COWARD!"

 
mmerlinn,

I don't think it would matter using cField<>m.cField or reversed.

I do think that testing a logical value is faster than comparing strings for every record that comes along.
And of course the value of the logical variable gets set .T. several times if a string contains more spaces, but I still think that's the fastest way. I haven't been testing on a table though...

But both ways should do the job.

 
Set Library to foxtools additive
Replace cField WITH Reduce(cField)

Bye, Olaf.
 
@borsker

Are you going to tell us what solution you are going to use?

Rob.
 
Almost good Olaf,
it should be:

Set Library to foxtools additive
Replace cField WITH Reduce(cField, space(1))

But a star from me for pointing us in this very simple solution!!!

Rob.
 
So my final solution would be:

Code:
Set Library to foxtools additive
use or select the table cField is in...
Scan
  If at(space(2),cField) > 0
    Replace cField WITH Reduce(cField, space(1))
  Endif
Endscan

Rob.
 

rob444

Testing the logical value might be faster, but like you, I did no tests to find out.

Regardless, unless the table is massive, or unless the code would be used on a continual basis, I don't think anyone would ever notice any difference in time between the two.

I tend to favor compact easily readable and easily understandable code regardless of speed of execution UNLESS it would make a noticeable difference in speed. And since speed of excution only becomes an issue in data processing loops, I ignore the issue elsewhere.

Anyhow stars to both you and Olaf.



mmerlinn

"Political correctness is the BADGE of a COWARD!"

 
Sorry it took so long to get back, things were kind of hectic the past week and I had to move to another project. I am back to it now and thank you to all for the posts.

Unfortunately I am using 6.0 and I am having problems with the code myearwood, mmerlinn, OlafDoschke and rob444 are using. Either 6.0 can not support it or it is operator error. I ended up using Shardlow code

REPLACE ALL cField WITH STRTRAN(cField, space(2), space(1))


 
Well borsker,

I just tried the code in vfp6 and it works just fine.

But you need to understand that you have to fill in the code of use a table yourself.

so, if your table is called MyTable the code looks like:

Code:
Use MyTable
and the rest of the code:
Set Library to foxtools additive
REPLACE cField WITH Reduce(alltrim(cField), space(1)) ;
  FOR at(space(2),alltrim(cField)) > 0

Rob.


 
And of course the line
and the rest of the code: needs to be removed from the code!!!

Rob.
 
I am getting the error on this line:
Set Library to foxtools additive

This is the error:
File 'foxtools.prg' does not exist

I just figured that vfp6 did not support this command
 

I just figured that vfp6 did not support this command

It definitely does.
It probably just cannot find the library.
It usually is in the same directory as your VFP
(check if foxtools.fll is in C:\Program Files\Microsoft Visual Studio\Vfp98).

Try this:
Code:
set library to (home()+'foxtools.fll') additive

 

You can also probably find FoxTools Help file,
should be C:\Program Files\Microsoft Visual Studio\Vfp98\Tools\FoxTolls.chm.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top