Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Is there a STRTRAN function which is not case sensitive
Message
From
30/04/2002 10:26:20
 
 
To
30/04/2002 09:33:33
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00650635
Message ID:
00650842
Views:
42
Hi Bob,

Your StrTranC program works very well unless tcReplace contains tcSearchfor. In this case it gets into an infinite loop. For instance, If you do
?Strtranc("This is a TEst OF Strtranc","Test","New Test")
will get into an infinite loop.

I made a 2 changes that fix this condition and allow it to work in all cases.
*	FUNCTION STRTRANC
*		A case-insensitive STRTRAN(). In other words, characters in either the
*		searched string or the string searched for are matched regardless of case, and
*		the replacement string inserted.

*	Parameters are in the same format as SUBSTR:
*						TYPE		REQ/OPT
*		tcSearched		C			REQ
*		tcSearchfor		C			REQ
*		tcReplace		C			OPT

*	Although some parameters are identified as required,
*	the routine recovers gracefully if they are of a different data type.
*	The developer may want to add notification in these cases

*	The function returns the value of the string as converted by the function.
FUNCTION   STRTRANC
PARAMETERS			tcSearched, tcSearchfor, tcReplace
LOCAL				llContinue, lcReturn, lcReplace, lnSearchForSize

lcReturn = IIF (VARTYPE (tcSearched) = "C", tcSearched, "")
IF NOT EMPTY (tcSearchfor) AND VARTYPE (tcSearchfor) = "C"
	llContinue = .T.
	lcReplace = IIF (VARTYPE (tcReplace) = "C", tcReplace, "")
	lnSearchForSize = LEN (tcSearchfor)

	lnStart = ATC (tcSearchfor, lcReturn)
	DO WHILE lnStart > 0
*!*		lcReturn = LEFT (lcReturn, lnStart-1) + lcReplace + SUBSTR (lcReturn, lnStart + lnSearchForSize)
*!* New line to fix infinite loop problem
		lcReturn = LEFT (lcReturn, lnStart-1) + chr(254)+chr(253)+chr(254) + SUBSTR (lcReturn, lnStart + lnSearchForSize)
*!*
		lnStart = ATC (tcSearchfor, lcReturn)
	ENDDO
*!* Added to do real replace to avoid infinite loop
lcReturn = strtran(lcReturn,chr(254)+chr(253)+chr(254),lcReplace)
*!*
ENDIF
RETURN lcReturn
Elmer
Previous
Reply
Map
View

Click here to load this message in the networking platform