Removes leading and trailing white space, or other specified characters, from a CHARACTER or LONGCHAR expression. The data type of the returned value matches the data type of the expression passed to the function.
An expression (a constant, field name, variable name, or expression) whose value is a CHARACTER or LONGCHAR. If expression is a case-sensitive variable, the AVM performs a case-sensitive trim. If expression is a LONGCHAR, the result is in the same code page.
A character expression that specifies the characters to trim from expression. If you do not specify trim-chars, the TRIM function removes spaces, tabs, line feeds, and carriage returns.
The following procedure displays a menu that you can use to display Customer and Order information. The option numbers are displayed with leading spaces. The TRIM function removes the leading white space so the menu selection can be easily evaluated.
r-trim.p
DEFINE VARIABLE menu AS CHARACTER NO-UNDO EXTENT 3. DO WHILE TRUE: DISPLAY " 1. Display Customer Data" @ menu[1] SKIP " 2. Order Data" @ menu[2] SKIP " 3. Exit" @ menu[3] SkIP WITH FRAME choices NO-LABELS. CHOOSE FIELD menu AUTO-RETURN WITH FRAME choices TITLE "Demonstration Menu" CENTERED ROW 10. HIDE FRAME choices. IF TRIM(FRAME-VALUE) BEGINS "1" THEN RUN r-dblnkc.p. IF TRIM(FRAME-VALUE) BEGINS "2" THEN RUN r-dblnko.p IF TRIM(FRAME-VALUE) BEGINS "3" THEN LEAVE. END. |
The following example reads a text file and breaks it into words. It assumes that all words are separated by at least one space character. It uses the TRIM function with one parameter to remove white space from the ends of each input line. It then uses the TRIM function with two parameters to remove any punctuation characters from each word.
r-trim2.p
DEFINE VARIABLE infile AS CHARACTER NO-UNDO FORMAT "x(60)" LABEL "Input File". DEFINE VARIABLE intext AS CHARACTER NO-UNDO. DEFINE VARIABLE next-space AS INTEGER NO-UNDO. DEFINE VARIABLE word AS CHARACTER NO-UNDO FORMAT "x(32)" LABEL "Words". /* Get the name of a text file and set input to that file. */. SET infile. INPUT FROM VALUE(infile). DO WHILE TRUE: /* Read the next line from the file. */ IMPORT UNFORMATTED intext. intext = TRIM(intext). DO WHILE TRUE: /* Find the next space character. If none found, find the end of string. */ next-space = INDEX(intext, " "). IF next-space = 0 THEN next-space = LENGTH(intext) + 1. /* If the string contains no (more) words, then read the next line. */ IF next-space = 1 THEN LEAVE. /* Pull the first word off the string. Remove any punctuation characters around it. */ word = SUBSTRING(intext, 1, next-space - 1). word = TRIM(word, ",.;:!? ~"~ '[]()"). intext = TRIM(SUBSTRING(intext, next-space + 1)). /* Display the word. */ DISPLAY word WITH DOWN FRAME x. DOWN WITH FRAME x. END. END. |