Skip to Content
Technical Articles
Author's profile photo Amy King

Code Snippet Series: Trim Leading and Trailing Whitespace

This post is part of a series on code snippets. The complete list of posts in the series is available in the document Code Snippets: A Blog Series.

ABAP keyword CONDENSE is useful to remove leading and trailing space characters from a character string, however CONDENSE does not recognize other non-printing whitespace characters such as carriage returns, newlines and horizontal tabs.

Regular expressions give us a useful tool to recognize a wide variety of whitespace characters. The regular expression abbreviation [[:space:]], alternatively written as \s, represents the character set for all non-printing blank characters including space, carriage return, newline and horizontal tab.

The method below removes first trailing then leading whitespace characters from a changing parameter a_string of type string. This code may be used in any ABAP routine but is particularly useful for post-processing of user input in Web Dynpro UI elements such as a TextEdit.


METHOD trim_whitespace.

  DATA lv_strlen TYPE i.
  DATA lv_char TYPE string.

* Remove trailing whitespace from a_string
  DO.
      lv_strlen = strlen( a_string ) - 1.
      lv_char = a_string+lv_strlen(1).

      FIND REGEX '[[:space:]]' IN lv_char.
      CASE sy-subrc.
          WHEN 0.
              SHIFT a_string RIGHT DELETING TRAILING lv_char.
          WHEN OTHERS.
              EXIT.
      ENDCASE.
  ENDDO.

* Remove leading whitespace from a_string
  DO.
      lv_char = a_string+0(1).

      FIND REGEX '[[:space:]]' IN lv_char.
      CASE sy-subrc.
          WHEN 0.
              SHIFT a_string LEFT DELETING LEADING lv_char.
          WHEN OTHERS.
              EXIT.
      ENDCASE.
  ENDDO.

ENDMETHOD.

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Stefano Cataldo
      Stefano Cataldo

      Hi Amy King,

      I really appreciate this blog. You helped me a lot  … I was in trouble both with the statements

      CONDENSE and with a combination of “DELETE LEFT LEADING space” and “DELETE RIGHT TRAILING space“. 

      Thanks a lot ?

      Stefano

      Author's profile photo Amy King
      Amy King
      Blog Post Author

      I'm glad it was helpful Stefano, thank you!

      Author's profile photo Neil Nicholls
      Neil Nicholls

      I have liked to use CONDENSE, but also want to preserve repeating spaces inside the string

      this method does that nicely.

      Is there a simple way to preserve the leading spaces if required?

      Author's profile photo Amy King
      Amy King
      Blog Post Author

      "SHIFT ... RIGHT DELETING TRAILING ..." removes trailing space at the end of a string.
      "SHIFT ... LEFT DELETING LEADING ..." removes leading space at the beginning of a string.

      To preserve leading spaces, avoid using the latter.

      The caret ^ character in regular expressions indicates "the start of the string" and can help you to identify whether a string contains whitespace at its beginning:

      ^[[:space:]]

      Similarly, the dollar sign $ in regular expressions indicates "the end of the string" and can help you to identify whether a string contains whitespace at its end.

      [[:space:]]$

      Author's profile photo Neil Nicholls
      Neil Nicholls

      "To preserve leading spaces, avoid using the latter."

      but this can introduce new leading spaces because the string has now been shifted right

      "  abc   "  =>  "     abc"

       

      all I can think of at the moment is to substitute leading whitespace with some other character, then shift right, shift left, then replace the original leading characters. which feels a bit messy