GetWordCount(), GetWordNum()

These two functions represent another step in moving the most useful items from FoxTools into the core language. GetWordCount() takes a string and tells you how many words it contains. GetWordNum() returns a specified word from a string.

Usage

nWords = GETWORDCOUNT( cString [, cDelimiters ] )
cWord = GETWORDNUM( cString, nWord [, cDelimiters ] )

Parameter

Value

Meaning

cString

Character

The string to be parsed.

cDelimiters

Character

A list of characters to use as word separators.

Omitted

Words are separated by space, tab (CHR(9)) and linefeed (CHR(10)).

nWord

Positive number

Return the specified word from the string. If there aren't that many words in the string, return the empty string.

0 or negative number

Return the empty string.

nWords

Numeric

The number of words in the string, based on the specified separators.

cWord

Character

The specified word from the string.


The FoxTools function library has long contained Words and WordNum functions that let you take strings apart word-wise. Apparently, the FoxPro team realized that these were pretty useful and decided to add them to the language. (Probably, they needed them for building one of the various tools that comes with VFP.)

Like ALINES(), these functions aren't limited to thinking about only the conventional ways of dividing up strings. You can specify whatever word separators you want by passing the optional cDelimiters parameter, making the functions useful for generic processing. (You can even specify letters or digits.) However, ALINES() is much, much faster, so we don't see ourselves using these two functions that way a lot.


The cDelimiters parameter specifies a list of separators, not a single multi-character separator. (We do wonder why the documentation group persists in abusing the word "delimiter." The parameter in question is a separator, not a delimiter, since it needn't occur at both ends of each word.)Once you specify an alternate delimiter, you must include the default delimiters if you want them used as well. That is, only the characters listed in the cDelimiters parameter are used, if that parameter is specified. This is different than the way ALINES() works. We're not sure why GetWordNum() doesn't do bounds checking on its second parameter (the word number). When you specify a value that's impossible (like 0 or a negative number) or more than the number of words in the string, it returns the empty string. We can't decide whether this is a good thing or a bad thing.

Example

? GETWORDCOUNT("Now is the time for all good men") && Returns 8
? GETWORDCOUNT("Red,yellow,blue",",")              && Returns 3
? GETWORDCOUNT("Red, yellow and blue", ",")        && Returns 2
? GETWORDCOUNT("Red, yellow and blue",", ")        && Returns 4
 
? GETWORDNUM("Now is the time for all good men", 2) 
     && Returns "is"
? GETWORDNUM("Red,yellow,blue",3,",")        && Returns "blue"
? GETWORDNUM("Red, yellow and blue", 2, ",") 
     && Returns " yellow and blue"
? GETWORDNUM("Red, yellow and blue", 2, ", ")
     && Returns "yellow"

See Also

ALines(), FileToStr(), FoxTools, StrExtract(), StrToFile()


Back to Table of Contents

Copyright © 2002-2018 by Tamar E. Granor, Ted Roche, Doug Hennig, and Della Martin. Click for license .