The HotDocs Computation Archive
Get Extra Help

0068 - Strip Punctuation

Description:

Strips all punctuation from a string or text variable, returning only letters, numbers and spaces.


• Code •

""
SET Temp-n TO LENGTH( TextVar )
SET LoopLimit-n[ Temp-n ] TO 1

REPEAT Loop
   SET Temp-t TO MID( TextVar, COUNTER, 1 )
   IF ( Temp-t >= "a" AND Temp-t <= "z" )
   OR ( Temp-t >= "A" AND Temp-t <= "Z" )
   OR ( Temp-t >= "0" AND Temp-t <= "9" )
   OR ( Temp-t = " " )
      RESULT + Temp-t
   END IF
   SET LoopLimit-n TO UNANSWERED
END REPEAT
 
 
Variation:

""
SET Temp-n TO LENGTH( TextVar )
SET LoopLimit-n[ Temp-n ] TO 1

REPEAT Loop
   IF "1234567890" +
      "abcdefghijklmnopqrstuvwxyz" +
      " "
      CONTAINS MID( TextVar, COUNTER, 1 )
         RESULT + MID( TextVar , COUNTER , 1 )
   END IF
   SET LoopLimit-n TO UNANSWERED
END REPEAT

• Explanation •

This computation strips punctuation marks from a string. Specifically, it walks through the string one letter at a time, keeping letters, numbers, and spaces and discarding everything else. It then returns the "stripped" version of the string. I tested it with the string "@#$Hello, (Wor*ld).!" and the computation returned "Hello World".

The computation requires two temporary variables, Temp-t and Temp-n -- a text and a number variable. It also requires a repeated dialog called Loop which has a single number variable called LoopLimit-n. As all three variables are temporary variables only, set their advanced options to "Ask only in dialog," "Don't warn if unanswered," and "Don't save in answer file." The loop is explained in Computation #0015: Loops Via REPEAT.

The variation uses a string of acceptable characters, and tests whether the character currently being examined is contained in that string. If it is, the character will be included. Otherwise it will be skipped. This script has the advantage of being very customizable -- simply add characters you want included to the string. It also has the advantage of not needing the temporary text variable Temp-t.

See also, Computation #0020: Remove Spaces, Computation #0130: Multi-line to Single-line, and Computation #0141: Clean Up Spaces.

 

• Model Template •

This template has everything you need set up and configured for you. It will work as-is, or can be adapted to your variable and dialog names. It contains: 1) sample Word and WordPerfect templates (or an Automator form) to demonstrate an implementation of the computation, 2) a component file containing the computation and all supporting dialogs and variables, and 3) instructions for adapting the computation for your use.

(Go to the download page)

 
 

• Contributors •

LegalCS   Anonymous