Remove Extra Spaces From an Answer

From HotDocs Wiki

Jump to: navigation, search

When a user answers a Text variable in the interview, unless the variable has a pattern attached to it, the user can type almost anything in the box. As the template developer, you may want to alter the user's answer to be more consistent with the rest of the document. One common change you may want to make is to remove extra spaces between sentences. For example, if you use only one space between sentences in the template, but a user enters two spaces between sentences in an answer, you can write a computation to remove the duplicate spaces.

The first example uses a WHILE loop to repeatedly examine the answer of a Text variable to see if it contains any duplicate spaces. If two spaces are found, the answer is modified to remove the second space. The process is repeated until all duplicate spaces are removed.

The second example uses the STRIP expression model to remove any leading or trailing spaces from an answer.

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


//Example 1
  SET Text Var TO FIRST( Text Var, POSITION( Text Var, "  " ) ) + 
  LAST( Text Var, LENGTH( Text Var ) - POSITION( Text Var, "  " ) - 1 ) 
//Example 2 (HotDocs 2006 and later)
STRIP(Text Var, " ", TRUE, TRUE)

Text Var Any Text variable.