Remove Hard Returns From an Answer

From HotDocs Wiki

Jump to: navigation, search

Sometimes, you may need to remove hard returns from a user's answer when it is merged into the assembled document. For example, if you use a multi-line Text variable for an address, the user may type the street address on the first line and the city, state, and ZIP code on the second line:

123 Main Street Anytown, USA 98765

However, if you want to insert the address in the middle of a sentence, the hard return should be removed and replaced with a comma and space:

The defendant lives at 123 Main Street, Anytown, USA 98765.

You can use a Computation variable to insert the user's answer in the document with the hard returns replaced by commas and spaces. The following example scripts first determine if the answer contains a hard return. If a hard return is found, the computation returns the answer with a comma and space in place of the hard return.

NOTE:

Although a hard return appears to be just one character, it is actually two characters. For example, after setting the Return variable to a hard return, if you use LENGTH(Return) to find the number of characters in the answer, HotDocs returns 2. In the example scripts below, when the hard return is removed from the answer, this means that two characters must be removed instead of one.

Example

//Example 1: This computation uses a WHILE expression to replace all hard returns in an
//answer with commas and spaces. (WHILE works only in HotDocs 6.2 and higher.)
 
Text Var 
SET Return TO "
" 
WHILE RESULT CONTAINS Return 
  FIRST(RESULT,POSITION(RESULT, Return) - 1) + ", " + 
  LAST(RESULT,LENGTH(RESULT ) - POSITION(RESULT, Return) - 1) 
END WHILE 
 
//Example 2: This computation works in all versions of HotDocs, but it only replaces the first
//hard return with a comma and space. To replace additional hard returns, you must adapt
//this script as needed.
 
Text Var 
SET Return TO "
" 
IF RESULT CONTAINS Return 
  FIRST(RESULT,POSITION(RESULT, Return) - 1) + ", " + 
  LAST(RESULT,LENGTH(RESULT ) - POSITION(RESULT, Return) - 1) 
END IF

Text Var Any multi-line Text variable, such as an address variable.
Return A temporary Text variable that is set to a hard return. (See Create a Temporary Variable.)