Thursday, April 3, 2008

SAP ABAP Splitting Character Strings

SAP ABAP Splitting Character Strings

A character string can be split into 2 or more smaller strings. This is done using the SPLIT statement. The
syntax for the same is shown below.

SPLIT <filed1> AT <separator> INTO <f1> ... <fn>.

Once the above statement is executed the system makes a search for the filed <field1> for the separator 
<separator> and the contents of <field1> are split at the separator that means the part before the separator 
is placed in <f1> and after the <separator> is placed in <f2> till <fn>.

Application:

This can be very well used while uploading a file separated by commas and placing content separated by commas 
into different fields.

Also note that to place the fragments into target fields the should be enough target fields to accommodate all 
the content. In case the contents are more then the last defined filed will contain the remaining values along 
with the <separator> ie the delimiter.

In case the split is carried out in such a way that all the contents of the string are placed in different 
fields then Sy-subrc is set to 0. Else sy-subrc is set to 4.

DATA: TESTSPLIT(40),
f1(10) VALUE '**********',
f2(10) VALUE '**********',
f3(10) VALUE '**********',
f4(10) VALUE '**********',
f5(10) VALUE '**********',
f6(10) VALUE '**********',
DEL(2) VALUE '!!'.

TESTSPLIT = 'Part1!!Part2!!Part3!!Part4!!Part5!!PART6'.
WRITE TESTSPLIT.
skip.

SPLIT TESTSPLIT AT DEL INTO f1 f2 f3 f4 f5 f6.

WRITE / f1.
WRITE / f2.
WRITE / f3.
WRITE / f4.
WRITE / f5.
WRITE / f6.

The output appears as follows:

Split                                     
_________________________________________                                          
Part1!!Part2!!Part3!!Part4!!Part5!!PART6  
                                          
Part1                                     
Part2                                     
Part3                                     
Part4                                     
Part5                                     
PART6                                     


Note that the contents of the fields f1 ...f6 are totally overwritten and that they are filled out with 
trailing blanks.

You can also split a string into the individual lines of an internal table as follows:

SPLIT <STRING> AT <del> INTO TABLE <itab>.

The system adds a new line to the internal table <itab> for each part of the string.