TRIM

The TRIM function returns the given string with any leading and trailing blacks that are removed, or the given string with any leading and trailing specified characters removed.

The type of the function is alphanumeric, DBCS, or national depending on the class of its argument.

Format

Read syntax diagramSkip visual syntax diagramFUNCTION TRIM(argument-1 argument-2)
argument-1
Must be a nonnumeric literal or data item of class alphabetic, alphanumeric, DBCS, or national. Argument-1 identifies the source string for the trim.
argument-2
If specified, it must be a nonnumeric literal or data item of the same class as argument-1. It specifies the characters to trim off. If not specified, the trim character defaults to blank.

If argument-2 is not specified, the returned value is an alphanumeric, DBCS, or national character string that consists of the characters of argument-1 with any leading and trailing blanks removed. The blank character is a 1-byte space character (' ' or X'20') when argument-1 is of class alphanumeric, or one double-byte space (X'2020') when argument-1 is of class DBCS, or one national space (X'2000' or X'0030') when argument-1 is of class national.

If argument-2 is specified, all characters in argument-2 are trimmed off from both ends of the string. The returned value is an alphanumeric, DBCS, or national character string that consists of the characters of argument-1 with any leading and trailing characters specified in argument-2 removed.

The length of the returned string depends on the content and the class of argument-1. It is the length of the returned string in number of character positions. If argument-1 is a DBCS or national data item, the length is in DBCS or national character positions.

Returned values

The order of the characters in the argument-2 parameter does not affect the outcome of the operation. The characters are a list of single characters. For example, FUNCTION TRIML(fld, "abc") returns the substring of fld that begins with any character that is not 'a', 'b', or 'c'. If fld contains "caxyz", FUNCTION TRIM(fld, "abc") returns "xyz". Characters can appear twice in the second parameter with no error. For example, FUNCTION TRIM(fld, "aba") is valid. This means the same as FUNCTION TRIM(fld, "ab").

If the second parameter of FUNCTION TRIM, TRIML, or TRIMR is specified, blanks are not trimmed unless a blank appears as part of argument-2. TRIM, TRIML, and TRIMR functions are not sensitive to mixed SBCS/DBCS strings. Both argument-1 and argument-2 are treated as SBCS if their class is alphanumeric.

Examples:


      FUNCTION TRIM("xxxABxCxxx", "x")             // returns 'ABxC' 
      FUNCTION TRIMR(">>>>ABC<<<<<", "<>")         // returns '>>>>ABC' 
      MOVE "xyz" TO tc. 
      FUNCTION TRIML("xxyyzzyyzzABCxyzyxzxy", tc)  // returns 'ABCxyzyxzxy'