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

spliting a string into internal table

Former Member
0 Likes
10,131

I'm having a string like this str = 'aaa, bbbb, cc, dddddd, bbb,cccccc,dd,eee,,,,,,,,,any number'

I want to split the string at comma and send these records into my internal table which is containing four fields.

my internal table records should be like this aaa bbbb cc dddddd

bbb ccccc dd eee

how to achieve this please help me out..

<removed_by_moderator>

regards

swapna

Edited by: Julius Bussche on Feb 17, 2009 9:00 AM

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
3,237

Hi,

This is tested and is working fine....

data string type string value 'aaa, bbbb, cc, dddddd, bbb,cccccc,dd,eee'.
data string2 type string.
data string3 type string.
data : begin of fs,
col1(20) type c,
col2(20) type c,
col3(20) type c,
col4(20) type c,
end of fs.

data itab like table of fs.
string2 = string.

while string2 <> space.
split string2 at ',' into fs-col1 fs-col2 fs-col3 fs-col4 string2.
append fs to itab.
clear fs.
endwhile.

loop at itab into fs.
write / fs.
endloop.

Regards,

Siddarth

14 REPLIES 14
Read only

Former Member
0 Likes
3,237

Hi,

Use split statement.

split string at ',' into w_str.

fs-field1 = w_str.

Read only

0 Likes
3,237

can u pls write the code for that...

my string may contain any number of values like aa,bb,cc,dd,ee,ff,gg,hh,,,,,,,

my itab is containing four fields..

data : begin of itab occurs 0,

f1(2),

f2(2),

f3(2),

f4(2),

end of itab

it should split till the last comma and populate into my itab like this

aa bb cc dd

ee ff gg hh

i know how to use split command...but im unable to get this logic...

Edited by: Swapna Sharma on Feb 17, 2009 8:46 AM

Read only

0 Likes
3,237

Hi,

Have a look at the following Sample Code hope will solve out your problem,

SPLIT it_crow AT ',' INTO zfsl_stinfo-mandt
                              zfsl_stinfo-st_id
                              zfsl_stinfo-st_n
                              zfsl_stinfo-st_fn
                              zfsl_stinfo-st_reg
                              zfsl_stinfo-st_ph
                              zfsl_stinfo-st_pm.

Please Reply if any Issue,

Kind Regards,

Read only

0 Likes
3,237

Hi,

Test the following Code i think it will help you to solve out your problem,

DATA: str TYPE string.

str = 'AAA BBB CCC, AAA BBB CCC, AAA BBB CCC, AAA BBB CCC'.
DATA : BEGIN OF itab OCCURS 0,
f1(20),
f2(20),
f3(20),
f4(20),
END OF itab.

SPLIT str AT ',' INTO itab-f1
                      itab-f2
                      itab-f3
                      itab-f4.
APPEND itab TO itab.

LOOP AT itab INTO itab.
  WRITE: / itab-f1, itab-f2, itab-f3, itab-f4.
ENDLOOP.

Please Replay if still any Issue Waiting for your Reply,

Kind Regards,

Faisal

Read only

0 Likes
3,237

Hi,

Test the following Code too but be care full about the OFFSET in the LOOP.other wise will face Dump

Following Lines of Code are working fine.

DATA: str TYPE string,
      slen TYPE i,
      count TYPE i.

str = 'AA,BB,CC,DD,EE,FF,GG,HH,II,JJ,KK,LL,'.
DATA : BEGIN OF itab OCCURS 0,
f1(2),
f2(2),
f3(2),
f4(2),
END OF itab.

slen = STRLEN( str ).
slen = slen / 12 .
BREAK-POINT.
count = 0.
DO slen TIMES.
  SPLIT str+count(12) AT ',' INTO itab-f1
                        itab-f2
                        itab-f3
                        itab-f4.
  APPEND itab TO itab.
  add 12 to count.
ENDDO.


LOOP AT itab INTO itab.
  WRITE: / itab-f1, itab-f2, itab-f3, itab-f4.
ENDLOOP.

also test may Sample Code in the following Thread,

[Splitting a string in different substrings |]

Kind Regards,

Faisal

Edited by: Faisal Altaf on Feb 17, 2009 1:06 PM

Read only

0 Likes
3,237

Hi,

try this .

data w_i type i.

w_i = strlen( string ).

subtract 1 from w_i.

while string+w_i(1) <> ' ' .

split string at ',' into itab-f1 itab-f2 itab-f3 itab-f4.

append itab.

clear itab.

endwhile.

Read only

0 Likes
3,237

thanks to all...its solved..

Read only

Former Member
0 Likes
3,237
Read only

Former Member
0 Likes
3,237

hi swpana,

Split -

data : a(10),b(10),c(10),d(40).

D = u2018Apple/Orange/Bananau2019.

Split d at u2018/u2019 into a b c.

Write:/ a,

/ b,

/ c.

you can refer this abap keyword for splitin the strings.

u hv to just replace / with ,(comma) in your case.....

Reagards

Raj

Read only

Former Member
0 Likes
3,237

try this reference.


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. 

regards,

Mon Magallanes

Read only

Former Member
0 Likes
3,237

HI Swapna


DATA itab TYPE TABLE OF string.
SPLIT string AT ',' INTO TABLE itab.

LOOP AT itab.
CASE counter.
WHEN 0.
itab1-field1 = itab.
ADD 1 TO counter.
WHEN 1.
itab1-field2 = itab.
ADD 1 TO counter.
WHEN 2.
itab1-field3 = itab.
ADD 1 TO counter.
WHEN 3.
itab1-field4 = itab.
APPEND itab1.
CLEAR counter.
ENDCASE.
ENDLOOP.

Pushpraj

Read only

Former Member
0 Likes
3,238

Hi,

This is tested and is working fine....

data string type string value 'aaa, bbbb, cc, dddddd, bbb,cccccc,dd,eee'.
data string2 type string.
data string3 type string.
data : begin of fs,
col1(20) type c,
col2(20) type c,
col3(20) type c,
col4(20) type c,
end of fs.

data itab like table of fs.
string2 = string.

while string2 <> space.
split string2 at ',' into fs-col1 fs-col2 fs-col3 fs-col4 string2.
append fs to itab.
clear fs.
endwhile.

loop at itab into fs.
write / fs.
endloop.

Regards,

Siddarth

Read only

Former Member
0 Likes
3,237

Hi,

Use FM CONVERT_STRING_TO_TABLE , UACC_CONVERT_STRING_TO_TABLE

to convert it into table but here you must be sure of the field length. if that is not the case

while forming your string concatenate various values using a separator and then split using

the same separator into table SPLIT i_tab_data AT ';' INTO TABLE <fs_itab>.

Read only

Former Member
0 Likes
3,237

Hello Sharma, try this

DATA: BEGIN OF t_split OCCURS 0,

         vkorg TYPE char4,

       END OF t_split.

RANGES: r_vkorg FOR vbrk-vkorg.

"TYPE TABLE OF char4 WITH HEADER LINE.

REFRESH: t_split[] , r_vkorg[].

SPLIT 'OV01,OV02,OV07,' AT ',' INTO TABLE t_split.

LOOP AT t_split.

   CLEAR r_vkorg.

   r_vkorg-sign   = 'I'.

   r_vkorg-option = 'EQ'.

   r_vkorg-low    = t_split-vkorg.

   APPEND r_vkorg.

ENDLOOP.