ABAP Character string division: SPLIT Command

  Define Data

Over View

The SPLIT instruction is an instruction that divides the value by a specific character.
Get data from a file such as tab delimited or comma delimited
It is often used when you want to assign a value to each item.
It will also be used when you have a mechanism to interface using XML files.

Sample code: comma separated

DATA: V_CHAR(15) TYPE C,
      V_COL1(5)  TYPE C,
      V_COL2(5)  TYPE C,
      V_COL3(5)  TYPE C.

V_CHAR = 'ABC;DEF;GHI'.
SPLIT V_CHAR AT ';' INTO V_COL1 V_COL2 V_COL3.

Explain

In this example, the value’ABC; DEF; GHI ‘can be assigned to the variables: V_COL1, V_COL2, V_COL3 by using the SPLIT instruction.
V_COL1 is assigned’ABC ‘, V_COL2 is assigned’DEF’, V_COL3 is assigned’GHI ‘, and is used as a delimiter; (semicolon) is removed.

Sample code: multiple delimiters

DATA: V_CHAR(15) TYPE C,
      V_COL1(15)  TYPE C,
      V_COL2(15)  TYPE C,
      V_COL3(15)  TYPE C.

V_CHAR = 'ABC;DEF,GHI'.
SPLIT V_CHAR AT ';' INTO V_COL1 V_COL2.
* V_COL1 = ABC V_COL2 = DEF,GHI

SPLIT V_COL2 AT ',' INTO V_COL2 V_COL3.
* V_COL2 = DEF V_COL3 = GHI

Explain

In this example, multiple delimiters are used, so let’s explain the case where it cannot be obtained by using the SPLIT command at once.
The values cannot be grouped together like the first example because we want to separate them with; (semicolon) and (comma) like’ABC; DEF, GHI ‘.
In such a case, it is time-consuming, but we recommend separating them one by one.
First, you can assign’ABC ‘to V_COL1 by separating with; (semicolon). At this time, the remaining value in V_COL2 is’DEF, GHI ‘
Remains, so do the same again, separating with, (comma). Then V_COL2 will be assigned’DEF ‘and V_COL3 will be assigned’GHI’.