‎2008 May 21 6:59 AM
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.
‎2008 May 21 7:08 AM
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
‎2008 May 21 7:04 AM
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.
‎2008 May 21 7:05 AM
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.
‎2008 May 21 7:07 AM
Thanks Neelambari and Sagar.
Your answers are helpful. But what if I dont know how many delimiters are there in string1?
‎2008 May 21 7:16 AM
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
‎2008 May 21 7:08 AM
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
‎2008 May 21 7:21 AM
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.
‎2008 May 21 7:30 AM
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