Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Odd string replacement: STRTRAN but with embedded spaces
Message
From
14/11/2006 11:10:12
 
 
To
14/11/2006 10:22:39
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01169359
Message ID:
01169508
Views:
9
Greg --

I think your attempt works fine in the case where there is at least one non-blank character after the embedded UPC Code. If however, the string is "Small Widget 1 12345 12345 1", it returns spurious results.

I believe this is caused by your looping; lnPos should only try to move to the next non-blank character if there are characters left to be searched for. (i.e., in all cases except the last character)

>Here's my attempt:
>
>
>LOCAL lcString, lcUPC
>lcString = "Small Widget (1 12345 12345 1)"
>lcUPC = "112345123451"
>
>lcRemoved = RemoveUPC(lcString,lcUPC)
>CLEAR
>?lcString
>?lcRemoved
>
>FUNCTION RemoveUPC
>LPARAMETERS pcString,pcUPC
>LOCAL lnNdx, lcRemoved, lnPos, lnUPC, llFound
>lcRemoved = pcString
>lnNdx = ATC(pcUPC,pcString)
>IF lnNdx > 0
>	lcRemoved = STUFF(pcString,lnNdx,LEN(pcUPC),"")
>ELSE
>	llFound = .F.
>	FOR lnNdx=1 TO LEN(pcString)
>		IF SUBSTR(pcString,lnNdx,1) = LEFT(pcUPC,1)
>			lnPos = lnNdx
>			lnUPC = 1
>			DO WHILE lnUPC<=LEN(pcUPC) .AND. SUBSTR(pcString,lnPos,1) = SUBSTR(pcUPC,lnUPC,1)
>				lnUPC = lnUPC + 1
>				IF lnUPC > LEN(pcUPC)
>					llFound = .T.
>					EXIT
>				ENDIF
>				lnPos = lnPos + 1
>				DO WHILE SUBSTR(pcString,lnPos,1) = " "   && Check for embedded spaces -- skip past
>					lnPos = lnPos + 1
>				ENDDO
>			ENDDO
>		ENDIF
>		IF llFound
>			lcRemoved = STUFF(pcString,lnNdx,lnPos-lnNdx+1,"")
>			EXIT
>		ENDIF
>	ENDFOR
>ENDIF
>RETURN lcRemoved
>ENDFUNC
>
Jim Nelson
Newbury Park, CA
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform