Code examples · Modern ABAP · Beginner
ABAP strings: split, clean, validate with a regex and format a number
The problem
A file contains lines like ' SD-0001 ; Acme GmbH ; 1.250,50 '.
How we solve it
- 1Split and trimSPLIT, CONDENSE
- 2ValidateMATCHES regex
- 3Convert and printREPLACE, template
The result
A messy line becomes three clean values and a formatted result, and a wrong id is reported.
1. The requirement
A file contains lines like ' SD-0001 ; Acme GmbH ; 1.250,50 '. Each line must be split into order id, customer and amount. The order id must match the pattern two capital letters, a hyphen and four digits. The amount uses European format and must become a number.
2. How we design it
Process one line in small steps, each doing one thing: split into parts, trim each part, validate the id, convert the amount text, then print a clean result. Built-in string functions replace long manual code, and a string template builds the output.
Why we decided it this way
- 1SPLIT into an internal tableOne statement gives all the parts, however many there are.
- 2A regular expression for the idThe pattern says exactly what is allowed, in one line.
- 3Two REPLACE calls for the amountRemove the thousands dot first, then turn the decimal comma into a dot, so the text can be converted to a number.
Before you start
- ADT and a console class. The code runs with F9, no database needed.
3. The code, step by step
Step 1 of 3: Split the line and trim the parts
Get three clean text parts.
" Console class and the input
CLASS zcl_string_demo DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
ENDCLASS.
CLASS zcl_string_demo IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
DATA(line) = ` SD-0001 ; Acme GmbH ; 1.250,50 `.
" SPLIT and condense
SPLIT line AT ';' INTO TABLE DATA(parts).
DATA(id) = condense( parts[ 1 ] ).
DATA(customer) = condense( parts[ 2 ] ).
DATA(amount_text) = condense( parts[ 3 ] ).
Part by part (plain text)
- Console class and the input. A console class runs with F9. The line is a string literal in backticks, which keeps leading and trailing blanks.
- SPLIT and condense. SPLIT cuts the line at every semicolon and puts the pieces into a table of strings. The function condense removes leading and trailing blanks and reduces inner runs of blanks to one.
Open in the portal
The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.
If it does not work
4. The final outcome
A messy line becomes three clean values and a formatted result, and a wrong id is reported.
What you should see
Bad id: XX-12 SD-0001 | Acme GmbH | 1250.50
Try it and check the result
- Run with F9. The console shows 'Bad id: XX-12' for the second test value, and the clean line for the real one.
- Change the input id to SD-00X1 and run: the first check fails and the program stops with 'Bad id'.
- Change the amount text to '99,5' and run: the result is 99.50, which shows the replace steps do not need a thousands dot.
Common mistakes
- A character literal in single quotes loses trailing blanks. Use backticks when blanks matter.
- parts[ 3 ] raises an exception if the line has fewer than three parts. For real files, check lines( parts ) first.
- Regular expressions are case sensitive by default. Write [A-Za-z] if lower case is allowed.
- On ABAP 7.55 and higher (and in ABAP Cloud) the POSIX addition regex = is obsolete: write pcre = pattern instead. This example uses regex = because it also runs on 7.54.
- Never convert a text with a comma decimal straight into a number. The system setting for decimals can differ between users.