ABAP CONDENSE: Remove Whitespace – But Watch Out for Trailing Blanks!

Category
ABAP-Statements
Published
Author
Johannes

The CONDENSE statement is used to remove unnecessary whitespace from a character-type data object (usually of type C - fixed-length string). It modifies the content of the specified variable directly in place.

Specifically, CONDENSE performs the following actions in its default behavior:

  1. Removes all leading blanks.
  2. Replaces sequences of multiple internal blanks (blanks between visible characters) with exactly one single blank.

Important: By default, CONDENSE does not remove trailing blanks at the end of the string!

Syntax

CONDENSE <variable> [NO-GAPS].
  • <variable>: The name of the character-type variable (usually type C) whose content is to be condensed.
  • [NO-GAPS] (Optional): An addition that changes the behavior: When NO-GAPS is specified, all blanks (leading, internal, and trailing) are completely removed from the variable.

Behavior in Detail

Without NO-GAPS (CONDENSE <variable>.)

  • " Hello World " becomes "Hello World " (if <variable> is e.g., C(30). The trailing blanks up to the field length remain.)
  • " " becomes " " (only blanks, left-aligned with blanks up to field length)
  • " ABC " becomes "ABC "
  • "A B C" becomes "A B C "

With NO-GAPS (CONDENSE <variable> NO-GAPS.)

  • " Hello World " becomes "HelloWorld " (rest filled with blanks)
  • " " becomes " " (empty field)
  • " ABC " becomes "ABC "
  • "A B C" becomes "ABC "

Applicability (Type C vs. STRING)

Type C (fixed-length string): This is the primary use case for CONDENSE. Type-C fields are often right-padded with blanks after assignments or may contain leading/multiple internal blanks through processing.

Type STRING (variable-length string): CONDENSE can also be applied to strings.

  • Standard CONDENSE removes leading blanks and reduces internal multiple blanks to one. Trailing blanks that are part of the string value are preserved.
  • CONDENSE ... NO-GAPS removes all blanks from the string.
  • For strings, there are often more specific and modern alternatives like SHIFT ... DELETING LEADING/TRAILING space., REPLACE, or string templates/functions that offer more control.

Examples

1. Standard CONDENSE with Type C

DATA text_c TYPE c LENGTH 40 VALUE ' First Part second Part '.
WRITE: / 'Type C Before :', |'{ text_c }'|.
CONDENSE text_c.
WRITE: / 'Type C After :', |'{ text_c }'|.

Output:

Type C Before : '{ First Part second Part }'
Type C After : '{First Part second Part }'

(Leading blanks removed, internal reduced to 1, trailing blanks remain up to length 40).

2. CONDENSE with NO-GAPS on Type C

DATA text_c_ng TYPE c LENGTH 40 VALUE ' First Part second Part '.
WRITE: / 'Type C NG Before :', |'{ text_c_ng }'|.
CONDENSE text_c_ng NO-GAPS.
WRITE: / 'Type C NG After :', |'{ text_c_ng }'|.

Output:

Type C NG Before : '{ First Part second Part }'
Type C NG After : '{FirstPartsecondPart }'

(All blanks removed, the rest is left-aligned).

3. CONDENSE on Type STRING

DATA text_s TYPE string VALUE ` String with lots of space `.
WRITE: / 'String Before :', |'{ text_s }'|.
CONDENSE text_s.
WRITE: / 'String After :', |'{ text_s }'|. " Leading removed, internal reduced, trailing remains
DATA text_s_ng TYPE string VALUE ` String with lots of space `.
CONDENSE text_s_ng NO-GAPS.
WRITE: / 'String After NG:', |'{ text_s_ng }'|. " All blanks removed

Output:

String Before : '{ String with lots of space }'
String After : '{String with lots of space }'
String After NG: '{Stringwithlotsofspace}'

Important Notes / Best Practice

  • CONDENSE modifies the passed variable directly. Save the original value beforehand if it’s still needed.
  • The most common misconception is that CONDENSE removes trailing blanks. This is false in the standard behavior. Only CONDENSE ... NO-GAPS removes all blanks.
  • To remove only trailing blanks, use SHIFT <str> RIGHT DELETING TRAILING space. for strings, or for type C, for example, assignment to a string and back, or special function modules.
  • CONDENSE is useful for cleaning up input fields or data from external sources that may contain unwanted blanks.
  • For complex string manipulations, modern string functions or operators are often a better choice than CONDENSE.