StrExtract()
StrExtract() was added in VFP 7, probably to help with parsing XML statements, though it's really useful for parsing other kinds of strings, too (GenMenu and the FoxCode app that makes IntelliSense work both use it quite a bit). StrExtract() searches for a pair of delimiters, then returns the text between those delimiters. This is a brute force way to slice-and-dice a string; StrExtract does not honor the Document Object Model (DOM). For example, if you have a nested XML document where the customers and the orders both have a <STATE> attribute, StrExtract can't differentiate between the two. It also doesn't support the attribute-based <Customer State="MA"> </Customer> formats.
Usage |
cText = STREXTRACT( cSearchIn , cStartDelimiter [ , cEndDelimiter [ , nOccurrence [ , nFlag ]]]]) |
Parameter |
Value |
Meaning |
cSearchIn |
Character |
The character string containing the text to search. |
cStartDelimiter |
Character |
The text indicating the starting delimiter. |
cEndDelimiter |
Character |
The text indicating the ending delimiter. |
Omitted |
No ending delimiter is specified; returns from the first delimiter to the end of the string. |
|
nOccurrence |
Numeric |
Specifies the occurrence of the delimiters between which the text is returned (the first, second, nth, and so on). |
Omitted |
The first occurrence of the text is returned. |
|
nFlag |
1 |
Indicates that the search is not case-sensitive. |
2 |
Indicates that the text specified in cEndDelimiter does not have to be found. |
|
3 |
Indicates that the search is not case-sensitive, and that the cEndDelimiter text does not have to be found. |
|
Omitted |
No change to behavior: Case-sensitive search, where the end delimiter must be found. |
|
cText |
Character |
The text between the specified delimiters. |
Empty string |
No match was found. |
Example |
* A simple example that ALINES() could handle just as easily: cList = "1, 2, 3, 4, 5" ? StrExtract(cList, ",", ",") && Returns " 2" * Parse an XML string. cMyXML = "<Data>" + ; "<CLASS>" + ; " <NAME>Introduction to Developing with VFP</NAME>" + ; " <NUMBER>7101</NUMBER>" + ; "</CLASS>" + ; "<CLASS>" + ; " <NAME>Developing with VFP</NAME>" + ; " <NUMBER>7201</NUMBER>" + ; "</CLASS>" + ; "</Data>" nTagCount = OCCURS("<NAME>", cMyString) FOR nOccurrence = 1 TO nTagCount ? STREXTRACT(cMyString, "<NAME>", "</NAME>", nOccurrence) ENDFOR |
See Also |
ALines(), At(), AtC(), CursorToXML(), GetWordCount(), GetWordNum(), Occurs(), FileToStr(), Rat(), RatC(), StrToFile(), StrTran(), SubStr(), SubStrC(), XMLToCursor() |