Remove Punctuation from the End of an Answer

From HotDocs Wiki

Jump to: navigation, search

Sometimes, users may enter punctuation at the end of an answer that does not work with the sentence into which the variable is merged. For example, suppose you add a variable field to the end of a sentence, before the period. If the user also enters a period in the answer, the assembled document would contain two periods.

The "Example 1" script below removes any punctuation, including spaces, from the end of an answer. The first version of the computation actually changes the user's answer. If you want to leave the answer unaltered, you can use the second version, which simply returns the value of the answer without the ending punctuation as the result of the computation.

The third version of the computation uses the WHILE instruction to remove punctuation from the end of an answer, even if it is more than just one character. For example, if the user enters two periods at the end of the answer, this computation removes both periods, but the other versions remove only the last period.

The "Example 2" script below removes a specified character or characters from the end of a text answer (because the punctuation is already included in the document text).

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

Example

//Example 1 (HotDocs 2005 and earlier)
 
//Version 1: Removes the last character from Text Var if it is a punctuation 
//mark or space.
IF ".!? " CONTAINS LAST( Text Var, 1 ) 
  SET Text Var TO FIRST( Text Var, LENGTH( Text Var ) - 1 ) 
END IF
 
//Version 2: Returns the answer of Text Var without the last character if it is a 
//punctuation mark or space.
""
Text Var 
IF ".!? " CONTAINS LAST( Text Var, 1 ) 
  FIRST( Text Var, LENGTH( Text Var ) - 1 ) 
END IF 
 
//Version 3: Removes the last characters from Text Var if they are punctuation 
//marks or spaces. (HotDocs 6.2) 
WHILE ".!? " CONTAINS LAST( Text Var, 1 ) 
  SET Text Var TO FIRST( Text Var, LENGTH( Text Var ) - 1 ) 
END WHILE 
 
//Example 2 (HotDocs 2006 and later)
 
STRIP(Text Var, " !.,?", FALSE, TRUE)

NOTE:

Because the FALSE and TRUE parameters are used, HotDocs strips the characters from the end of the answer (TRUE) and not the beginning (FALSE).

Text Var Any Text variable whose answer you want to search for spaces or other characters.
Text Var Any Text variable whose answer you want to search for spaces or other characters.