Wiki Home

String Extract


Namespace: VFP
This is a VFP6 version of VFP7's String Extract function. It does not deal with the 5th param: nFlags

FUNCTION STREXTRACT( tcSearchExpression, tcBeginDelim, tcEndDelim, tnOccurrence )

LOCAL lnPos1, lnPos2, lnLen, lnOccurrence, lcStrExtract

lnOccurrence = IIF( EMPTY( tnOccurrence ), 1, tnOccurrence )
IF EMPTY(tcBeginDelim)
  lnPos1 = 1
ELSE
  lnPos1 = at( tcBeginDelim, tcSearchExpression, lnOccurrence ) + len( tcBeginDelim )
ENDIF
lnLen  = at( tcEndDelim, substr( tcSearchExpression, lnPos1 ), 1 ) -1
lcStrExtract = substr( tcSearchExpression, lnPos1, lnLen )
RETURN lcStrExtract


---

I think this will handle the nFlag issue:
FUNCTION StringExtract
  LPARAM tcSearchExpression, tcBeginDelim, tcEndDelim, tnOccurrence, tnFlag

  LOCAL lnPos1, lnPos2, lnLen, lnOccurrence, lcStrExtract
  LOCAL lcSearch, lcBegin, lcEnd, lnFlag

  lnOccurrence = IIF( EMPTY( tnOccurrence ), 1, tnOccurrence )

  IF VARTYPE(tnFlag) ="N"
    lnFlag = tnFlag
  ELSE
    lnFlag = 0
  ENDIF


  IF BITAND(lnFlag,1) = 1
    lcSearch = LOWER(tcSearchExpression)
    lcBegin = LOWER(tcBeginDelim)
    lcEnd = LOWER(tcEndDelim)
  ELSE
    lcSearch = tcSearchExpression
    lcBegin = tcBeginDelim
    lcEnd = tcEndDelim
  ENDIF
  IF EMPTY(lcBegin)
    lnPos1 = 1
  ELSE
    lnPos1 = AT( lcBegin, lcSearch, lnOccurrence ) + LEN( lcBegin )
  ENDIF
  lnLen  = AT( lcEnd, SUBSTR( lcSearch, lnPos1 ), 1 ) -1

  IF lnLen = 0 AND BITAND(lnFlag, 2)=2
    lnLen = LEN(tcSearchExpression)-lnPos2
  ENDIF

  lcStrExtract = SUBSTR( tcSearchExpression, lnPos1, lnLen )

  RETURN lcStrExtract
ENDFUNC



Fixed bit 2 of nFlag, and added bit 3. Hopefully this is consistent with VFP9SP1... (but is not - it doesn't handle the optional parameters very well.)


FUNCTION StringExtract
	LPARAM tcSearchExpression, tcBeginDelim, tcEndDelim, tnOccurrence, tnFlag
	LOCAL lnPos1, lnPos2, lnLen, lnOccurrence, lcStrExtract
	LOCAL lcSearch, lcBegin, lcEnd, lnFlag
	LOCAL lnIBegin, lnIEnd
	STORE 0 TO lnIBegin, lnIEnd
	lnOccurrence = IIF(EMPTY(tnOccurrence), 1, tnOccurrence)
	IF VARTYPE(tnFlag) = "N"
		lnFlag = tnFlag
	ELSE
		lnFlag = 0
	ENDIF
	IF BITAND(lnFlag, 1) = 1
		lcSearch = LOWER(tcSearchExpression)
		lcBegin = LOWER(tcBeginDelim)
		lcEnd = LOWER(tcEndDelim)
	ELSE
		lcSearch = tcSearchExpression
		lcBegin = tcBeginDelim
		lcEnd = tcEndDelim
	ENDIF
	IF EMPTY(lcBegin)
		lnPos1 = 1
	ELSE
		IF BITAND(lnFlag, 4) = 4
			lnIBegin = LEN(lcBegin)
		ENDIF
		lnPos1 = AT(lcBegin, lcSearch, lnOccurrence) + LEN(lcBegin) - lnIBegin
	ENDIF
	IF BITAND(lnFlag, 4) = 4
		lnIEnd = LEN(lcEnd)
	ENDIF
	lnLen = AT(lcEnd, SUBSTR(lcSearch, lnPos1), 1) + lnIEnd - 1
	IF (lnLen = -1) AND (BITAND(lnFlag, 2) = 2)
		lnLen = LEN(tcSearchExpression) - lnPos1 + 1
	ENDIF
	lcStrExtract = SUBSTR(tcSearchExpression, lnPos1, lnLen)	
RETURN lcStrExtract


Contributors Carl Karsten, Josh Assing, RobinGuest
Category Code Samples Category VFP Functions
( Topic last updated: 2007.09.05 03:22:44 PM )