Click or drag to resize

StrExtract Function

X#
-- todo --
Retrieves a string between two delimiters.

Namespace:  XSharp.VFP
Assembly:  XSharp.VFP (in XSharp.VFP.dll) Version: 2.19
Syntax
 FUNCTION StrExtract(
	cSearchExpression,
	cBeginDelim,
	cEndDelim,
	nOccurrence,
	nFlag
) AS STRING CLIPPER
Request Example View Source

Parameters

cSearchExpression (Optional)
Type: Usual
Specifies the string to search.
cBeginDelim (Optional)
Type: Usual
Specifies the character that delimits the beginning of cSearchExpression.
cEndDelim (Optional)
Type: Usual
Specifies the character that delimits the end of cSearchExpression.
nOccurrence (Optional)
Type: Usual
Specifies at which occurrence of cBeginDelim in cSearchExpression to start the extraction.
nFlag (Optional)
Type: Usual
Specify the type of controls placed on the search. The number you specify in nFlag provides a bit-value that determines options according to the following table:
BitValue (additive)Description
01Case-insensitive search
12 End delimiter not required. Specifies that a search, which finds no occurrence of cEndDelim, returns the contents of cSearchExpression from the cBeginDelim location.
24Include the delimiters in the returned expression.

Return Value

Type: String
Character or Varbinary.br
The Result data type is derived by the first parameter data type.

Return Value

Type: String
Remarks
The default is a case sensitive search in which delimiters must be found (no nFlag value).
If cBeginDelim is an empty string, the search is conducted from the beginning of cSearchExpression to the first occurrence of cEndDelim. If cEndDelim is an empty string StrExtract( ) returns a string from nOccurrence of cBeginDelim to the end of cSearchExpression.
Examples
X#
 1Clear
 2Set PATH TO (HOME(2) + 'Data\')   //Set path to the customer table
 3Use customer // any table
 4?cursortoxml(0,"x",1,0,2)   // Produce variable "x" that has XML of  first 2 records of table
 5?x         // show the XML
 6xmlproc(x,0)      // Parse the XML
 7PROCEDURE xmlproc(x as String, nLev as Integer)   as void
 8LOCAL cTagName, cContents, mterm
 9DO WHILE .t.
10cTagName = StrExtract(x,"<",">")
11IF LEN(cTagName) = 0   // no tag found
12??' ',x   // print out raw string as contents
13EXIT
14ENDIF
15IF RIGHT(cTagName,1) = '/'   // like "<region/>"
16cTagName = LEFT(cTagName, LEN(cTagName)-1)
17cContents=""
18mterm = "<"+cTagName+"/>"   // "<region/>"
19ELSE
20mterm = "</"+cTagName+">"   // "</region>"
21cContents = StrExtract(x,"<"+cTagName+">", mterm,1,2)
22ENDIF
23?REPLICATE("  ",nLev),nLev+1,PADR(cTagName,20)
24xmlproc(cContents, nLev+1)
25x = StrExtract(x, mterm)   // get the rest of the xml
26ENDDO
See Also