Application Development and Automation 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: 
Read only

How to split string based on a delimiter

Former Member
0 Likes
16,165

Hi Experts,

Please help me know how I can split a string based on the delimiter.

For example, String1 contains the following

STRING1: George;Lewis;Williams;Hangman

I need to split STRING1 as the following

STRING2: George

STRING3: Lewis

STRING4: Williams

STRING5: Hangman.

Please let me know how this can be done. TIA.

Points shall be rewarded.

Regards,

Kris.

1 ACCEPTED SOLUTION
Read only

former_member435013
Active Participant
5,820

Hi,

try this:

REPORT ztest.

DATA:

x TYPE string,

itab(64) OCCURS 0 WITH HEADER LINE.

x = 'George;Lewis;Williams;Hangman'.

SPLIT x AT ';' INTO TABLE itab.

LOOP AT itab.

WRITE: / itab.

ENDLOOP.

regards

Walter Habich

7 REPLIES 7
Read only

Former Member
0 Likes
5,820

Hi..

try below logic..

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

NAMES2 TYPE STRING,

ONE(10) TYPE C,

TWO(10) TYPE C,

THREE TYPE STRING,

DELIMITER(2) VALUE ';'.

SPLIT NAMES AT DELIMITER INTO ONE THREE TWO.

Regards,

N M Poojari.

Read only

Former Member
0 Likes
5,820

Hi,

refer the below mentioned code.

DATA: str1 TYPE string,

str2 TYPE string,

str3 TYPE string,

str3 TYPE string.

-


str1v = `xxxx;xsss;dddd;rrrrr`.

SPLIT str1 AT ';' INTO: str2 str3 str4......

Reward if helpful.

Thanks.

Read only

0 Likes
5,820

Thanks Neelambari and Sagar.

Your answers are helpful. But what if I dont know how many delimiters are there in string1?

Read only

0 Likes
5,820

Hey Kris,

What do you mean by " how many delimiters in a string. "???

Is it occurance of delimiters many times or different types of delimiter like ';' , ',' or space in a string.

Please explain.

Regards,

N M poojari.

Edited by: Nilambari Poojari on May 21, 2008 8:16 AM

Read only

former_member435013
Active Participant
5,821

Hi,

try this:

REPORT ztest.

DATA:

x TYPE string,

itab(64) OCCURS 0 WITH HEADER LINE.

x = 'George;Lewis;Williams;Hangman'.

SPLIT x AT ';' INTO TABLE itab.

LOOP AT itab.

WRITE: / itab.

ENDLOOP.

regards

Walter Habich

Read only

Former Member
0 Likes
5,820

Hi,

If you are exactly aware of the strings after split, you can direcly use the SPLIT statement. If you are not sure about the number of occurrences of the delimiter, you need to add some logic like below:


DATA: BEGIN OF it_string OCCURS 0,
          strout type string,
          END OF it_string.
 l_text = 'George;Lewis;Williams;Hangman'.
 l_start = 0.
DO.
 FIND FIRST OCCURRENCE OF ';' IN l_text
                                      MATCH OFFSET l_offset.
 IF sy-subrc = 0.
     it_string-strout = l_text+l_start(l_offset - 1).
     APPEND it_string.
     l_start = l_start + l_offset.
     l_text = l_text+l_offset(l_length) "l_length is the maximum length of the string
 ELSE.
    EXIT.
 ENDIF.
ENDDO.

At the end of the DO loop, table it_string will have all the strings.

Thanks and Regards,

Lakshmi.

Read only

Former Member
0 Likes
5,820

hi see below example...

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!)

-Prabhu