Determine the Number of Spaces (or Other Characters) in an Answer

From HotDocs Wiki

Jump to: navigation, search

There may be times when you want to know how many spaces or other characters are contained in an answer. For example, by counting the number of spaces in an answer, you can determine how many words are in the answer. (The number of words is generally equivalent to the number of spaces, plus one.)

CAUTION: This computation uses the WHILE instruction, which works only in HotDocs 6.2 and higher.

You can use the following computation to determine the number of spaces or other characters in an answer. The computation's result begins with a value of zero and each time a space is found, it is incremented by one. A temporary Text variable is initially set to the answer of the variable whose spaces you want to count. Then, as each space is found, all characters up to and including the space are removed from the temporary variable's answer. In the end, the computation's result is the number of spaces that were found.

NOTE:

To search for and count other characters, replace each instance of " " with the other character. For example, to count the periods, use "." in the script.

Example

0 
SET Temp Var TO Text Var 
WHILE Temp Var CONTAINS " " 
  RESULT + 1 
  SET Temp Var TO LAST( Temp Var, LENGTH( Temp Var ) - POSITION( Temp Var, " " ) ) 
END WHILE

Text Var Any Text variable. The computation counts the number of spaces in this variable's answer.
Temp Var A temporary Text variable used for counting the number of spaces in the answer. (See Create a Temporary Variable.)