Progress
Language Reference


TRIM Function

Interfaces
OS
SpeedScript
All
All
Yes

Removes leading and trailing white space, or other specified characters, from a character string.

SYNTAX

TRIM ( string [ , trim-chars ] ) 

string

A character expression. The string can be defined as a constant, field name, variable name, or expression. If string is a case-sensitive variable, Progress performs a case-sensitive trim.

trim-chars

A character expression that specifies the characters to trim from string. If you do not specify trim-chars, the TRIM function removes spaces, tabs, line feeds, and carriage returns.

EXAMPLES

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 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 FORMAT "x(60)" LABEL "Input File".
DEFINE VARIABLE intext AS CHARACTER.
DEFINE VARIABLE next-space AS INTEGER.
DEFINE VARIABLE word AS CHARACTER 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. 

NOTES

SEE ALSO

LEFT-TRIM Function, RIGHT-TRIM Function


Copyright © 2004 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095