Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Split a string.

Former Member
0 Kudos

Hi,

In our project there is requirement wherein we have to split a string.can we split a string into n no. of parts.

Regards,

Suresh

1 ACCEPTED SOLUTION

Mohamed_Mukhtar
Active Contributor
0 Kudos

Hi Suresh,

See this example code ...

DATA: BEGIN OF file_all OCCURS 0,
          all(1000) TYPE c,
        END OF file_all.
CALL FUNCTION 'UPLOAD_FILES'
       EXPORTING
*           i_trunclen = ''
            i_filetype = 'ASC'
            i_xpc      = i_xpc
       TABLES
            file_all   = file_all
            tab_file   = tab_file
       EXCEPTIONS
            error_file = 1
            OTHERS     = 2.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  LOOP AT file_all.

    SPLIT file_all AT ';' INTO it_file-bankl
                               it_file-banka
                               it_file-provz
                               it_file-stras
                               it_file-ort01
                               it_file-swift
                               it_file-bnklz
                               it_file-brnch.

"ur code...........
Endloop.

Regards

8 REPLIES 8

GauthamV
Active Contributor
0 Kudos

hi,

use this syntax.

SPLIT f AT g INTO h1 ... hn.

check this excample provided by SAP.

Examples

DATA: NAMES(30) TYPE C VALUE 'Charly, John , Peter',

NAMES2 TYPE STRING,

ONE(10) TYPE C,

TWO(10) TYPE C,

THREE TYPE STRING,

FOUR(4) TYPE C VALUE 'FOUR',

DELIMITER(2) VALUE ','.

SPLIT NAMES AT DELIMITER INTO ONE TWO.

  • ONE contains 'Charly' and TWO contains 'John , Pet'.

  • SY-SUBRC is 4, because TWO was not large enough to

  • accommodate the whole of the remaining string

SPLIT NAMES AT ',' INTO ONE TWO THREE.

  • ONE contains 'Charly', TWO contains ' John',

  • THREE contains ' Peter'.

SPLIT NAMES AT ', ' INTO ONE THREE TWO.

  • ONE contains 'Charly', THREE contains 'John',

  • TWO contains 'Peter'.

CONCATENATE NAMES '' INTO NAMES2 SEPARATED BY SPACE.

SPLIT NAMES2 AT DELIMITER INTO ONE TWO THREE FOUR.

  • ONE contains 'Charly', TWO contains 'John',

  • THREE contains 'Peter ', FOUR is empty.

SPLIT NAMES2 AT DELIMITER INTO ONE FOUR THREE.

  • ONE contains 'Charly', FOUR contains 'John',

  • THREE contains 'Peter', SY-SUBRC is 4, since

  • FOUR was not large enough (spaces are significant

  • characters!)

Former Member
0 Kudos

HI,

You can Split a String In Any no of Parts.

You will find SPLIT syntax in Help document.

Regards

Sumit Agarwal

sachin_mathapati
Contributor
0 Kudos

Hi ,

Search in SDN with key word SPLIT or use F1 help in u r ABAP editor .

Also Check this link..

[http://help.sap.com/saphelp_46c/helpdata/EN/fc/eb3400358411d1829f0000e829fbfe/frameset.htm]

Regards,

Sachin M M

Former Member
0 Kudos

hi suresh,

see the code

DATA: NAMES(30) TYPE C VALUE 'Charly, John , Peter',

NAMES2 TYPE STRING,

ONE(10) TYPE C,

TWO(10) TYPE C,

THREE TYPE STRING,

FOUR(4) TYPE C VALUE 'FOUR',

DELIMITER(2) VALUE ','.

SPLIT NAMES AT DELIMITER INTO ONE TWO.

  • ONE contains 'Charly' and TWO contains 'John , Pet'.

  • SY-SUBRC is 4, because TWO was not large enough to

  • accommodate the whole of the remaining string

SPLIT NAMES AT ',' INTO ONE TWO THREE.

  • ONE contains 'Charly', TWO contains ' John',

  • THREE contains ' Peter'.

SPLIT NAMES AT ', ' INTO ONE THREE TWO.

  • ONE contains 'Charly', THREE contains 'John',

  • TWO contains 'Peter'.

CONCATENATE NAMES '' INTO NAMES2 SEPARATED BY SPACE.

SPLIT NAMES2 AT DELIMITER INTO ONE TWO THREE FOUR.

  • ONE contains 'Charly', TWO contains 'John',

  • THREE contains 'Peter ', FOUR is empty.

SPLIT NAMES2 AT DELIMITER INTO ONE FOUR THREE.

  • ONE contains 'Charly', FOUR contains 'John',

  • THREE contains 'Peter', SY-SUBRC is 4, since

  • FOUR was not large enough (spaces are significant

  • characters!)

Regards,

Mahesh.

Former Member
0 Kudos

Hi suresh Kumar,

Welcome to SDN.

You can split a string into parts as follows.

Example:

DATA:

string(50) TYPE c VALUE 'Venkat,Srinivas,Bhrammam',

w_variable1(10) TYPE c,

w_variable2(10) TYPE c,

w_variable3(10) TYPE c,

partition TYPE c VALUE ','.

SPLIT string AT partition INTO w_variable1 w_variable2 w_variable3.

WRITE:

/ w_variable1,w_variable2,w_variable3.

Hope this helps you.

Regards,

Rama.

Former Member
0 Kudos

Splitting Character Strings

To split a character string into two or more smaller strings, use the SPLIT statement as follows:

SPLIT <c> AT <del> INTO <c1> ... <cn>.

The system searches the field <c> for the separator <del>. The parts before and after the

separator are placed in the target fields <c1> ... <cn>.

To place all fragments in different target fields, you must specify enough target fields. Otherwise,

the last target field is filled with the rest of the field <c> and still contains delimiters.

If all target fields are long enough and no fragment has to be truncated, SY-SUBRC is set to 0.

Otherwise it is set to 4.

DATA: STRING(60),

P1(20) VALUE '++++++++++++++++++++',

P2(20) VALUE '++++++++++++++++++++',

P3(20) VALUE '++++++++++++++++++++',

P4(20) VALUE '++++++++++++++++++++',

DEL(3) VALUE '***'.

STRING = ' Part 1 *** Part 2 *** Part 3 *** Part 4 *** Part 5'.

WRITE STRING.

SPLIT STRING AT DEL INTO P1 P2 P3 P4.

WRITE / P1.

WRITE / P2.

WRITE / P3.

WRITE / P4.

The output appears as follows:

Part 1 *** Part 2 *** Part 3 *** Part 4 *** Part 5

Part 1

Part 2

Part 3

Part 4 *** Part 5

Note that the contents of the fields P1 ...P4 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 <c> AT <del> INTO TABLE <itab>.

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

Mohamed_Mukhtar
Active Contributor
0 Kudos

Hi Suresh,

See this example code ...

DATA: BEGIN OF file_all OCCURS 0,
          all(1000) TYPE c,
        END OF file_all.
CALL FUNCTION 'UPLOAD_FILES'
       EXPORTING
*           i_trunclen = ''
            i_filetype = 'ASC'
            i_xpc      = i_xpc
       TABLES
            file_all   = file_all
            tab_file   = tab_file
       EXCEPTIONS
            error_file = 1
            OTHERS     = 2.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  LOOP AT file_all.

    SPLIT file_all AT ';' INTO it_file-bankl
                               it_file-banka
                               it_file-provz
                               it_file-stras
                               it_file-ort01
                               it_file-swift
                               it_file-bnklz
                               it_file-brnch.

"ur code...........
Endloop.

Regards

Former Member
0 Kudos

Hi Suresh,

Below is the syntax:

SPLIT <c> AT <del> INTO <c1>... <cn> INTO TABLE <itab>

[IN BYTE MODE|IN CHARACTER MODE].

This statement searches the character field <c> for delimiter strings <del> and the parts before and after the delimiters are placed in the target fields <c1> ...> u2026 <cn>, or into a new line of the internal table <itab>. In Unicode programs, you must specify whether the statement is a character or byte operation, using the IN BYTE MODE or IN CHARACTER MODE (default) additions.

Try to use this if this could help you.

Regards,

Ashutosh