Example SPLIT in SAP ABAP

How to split a string in SAP ABAP ? Using SPLIT function to divide a string at a value into different variables

SPLIT

Syntax 

SPLIT dobj AT sep INTO
{ {result1 result2 ...} | {TABLE result_tab} }
[IN {CHARACTER|BYTE} MODE].

Addition: 


… IN {CHARACTER|BYTE} MODE

Effect 

The content of the operand dobj is separated into segments in accordance with the order of separators in sep. The results are saved either to individual character-like or byte-like variables result1 result2 … or to the rows of an internal table result_tab with character-like or byte-like row type. At least two variables result1 result2 … must be specified. dobj and sep are character-like expression positions. The internal table result_tab must be a standard table without secondary table keys and is initialized before the content is separated. 

The system searches the operand dobj from left to right for all occurrences of the content of the operand sep. The search is case-sensitive. All segments from the start of the operand to the first occurrence, between the occurrences, and from the last occurrence to the end of the operand are assigned one by one to the individual data objects result1 result2 …, or appended to the internal table result_tab. 

If the data objects result1 result2 … or the rows of the internal table result_tab have a fixed length and this length is not enough for the segment, the segment is truncated on the right and sy-subrc is set to 4. If the length is greater than the length of the segment, it is padded with blanks or hexadecimal 0 on the right. If the data objects result1 result2 … or the rows of the internal table result_tab are strings, their length is adjusted to match the length of the associated segment.

If there are not enough data objects result1 result2 … to record all the segments, dobj is only separated until all the data objects result1 result2 … have been assigned values, except for the last data object. The remaining content of dobj is assigned to the final data object, without being separted.

If more data objects result1 result2 … are specified than required, the surplus target fields with fixed lengths contain blanks or hexadecimal 0 after the assignment. Any surplus strings are initial. 

If the content of the operand sep is found immediately at the start of dobj, or occurs in direct succession in dobj, the result of the separation is an empty string. If the content of sep is at the end of dobj, the search is terminated and no further separation takes place to the right of this point. 

If the content of the operand sep is not found or is an empty string, the result of the separation is a single segment that contains the whole content of dobj, and which is assigned to the first individual data object or the first row of the internal table. 

In character string processing, the trailing blanks are respected for separators sep of fixed length, but not in the operand dobj or in the segments produced by the separation.

System Fields 

sy-subrcMeaning
0The segments were passed to the target fields or the internal table without being truncated.
4At least one of the segments was truncated on the right when being passed to the target fields or internal table.

Notes 

If enough target fields are specified or the segments have been saved to an internal table, the number of segments created is defined by the number of separators found, as follows: 

If the last occurrence is not at the end of the operand, the number of segments matches the number of occurrences plus 1. 

If the last occurrence is at the end of the operand, the number of segments matches the number of occurrences. 

All single fields result1 result2 … specified are given values. If these fields have the type n, d, or t in string processing, the type-friendly assignment rules and initial values are not relevant.

To access the segments of a character string directly in an operand position, you can use a segment function that includes some of the functions of the statement SPLIT.

Addition 

… IN {CHARACTER|BYTE} MODE 

Effect 

The optional IN {CHARACTER|BYTE} MODE addition determines whether character string or byte string processing is carried out. If the addition is not specified, character string processing is carried out. Depending on the type of processing, the operands dobj, sep, and the target fields result1 result2 … or the rows of the internal table result_tab must be byte-like or character-like.

Example 

The text field text is separated at its blanks, firstly into the three strings str1, str2, and str3, and then into an internal table with the row type string. Since the three strings are not enough for all seven parts, str3 contains “drag it is getting old” after the separation, while the internal table contains seven rows; one for each word in text. 

DATA: str1 TYPE string,
str2 TYPE string,
str3 TYPE string,
itab TYPE TABLE OF string,
text TYPE string.

text = `What a drag it is getting old`.

SPLIT text AT space INTO: str1 str2 str3,
TABLE itab. 

Example of using SPLIT into internal table

Instead of splitting the data into individual fields we can split data into an internal table.

DATA : LV_STRING TYPE STRING .
TYPES: BEGIN OF TY_STRING,

      STR(25) TYPE C,
      END OF TY_STRING.
DATA IT_STRING TYPE TABLE OF TY_STRING.
DATA WA_STRING TYPE TY_STRING .
LV_STRING = 'SPLIT ME AT SPACE'.
SPLIT LV_STRING AT ' ' INTO TABLE IT_STRING .

LOOP AT IT_STRING INTO WA_STRING.
  WRITE :/ WA_STRING-STR.
ENDLOOP.