Remove special characters from strings in ABAP.

Jimbo's picture

Data sometimes comes with special characters that are expressly forbidden by SAP. Some fields are very specific and will except a very small character set. Having a tool to remove those special characters from free-text can save a lot of headaches, but removing those same forbidden characters from fields that are not free-text, can cause more headaches.

In a recent implementation, data arrived with special characters that looked just like spaces in the classification name. These special characters defy the VBA trim function and the ABAP condense statement. The easiest way to scrape out these special characters is with a small snippet of code. In this case the decision to cleanse the text in SAP was chosen because the transformation of the object was being done by another group and only the loading was done by the data conversion team.

This snippet can be modified to contain any series of letters, numbers or characters. This code is specialized for classification names.

This snippet of code goes well in the User-Defined Routines section of a LSMW project. From there it can be accessed by any object in any sub-project.

form CleanseText using inValue changing outValue.
  data: nLength type i, cTemp(100) type c.
  translate inValue to UPPER CASE.
  nLength = strlen( inValue ).

  do nLength times.
    subtract 1 from nLength.
    if 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_' cs inValue+nLength(1).
      concatenate inValue+nLength(1) cTemp into cTemp.

  outValue = cTemp.
Programming Language: