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

Break string into itab string/char array

Former Member
0 Likes
3,858

Hi expert,

I know this question is a bit silly but i really cant find a way to do it.

Suppose i have a string 'ABCDE'

I want to store it as an itab with 5 rows:

A

B

C

D

E

DATA: SPLIT_SEQ type table of string with header line.

SPLIT STR_TO_BE_SPLIT AT '' INTO TABLE SPLIT_SEQ.   ' this line fail to perform what i need to do

can anyone help me on this?

Thanks so much !

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,154

Hi Christopher,

Try using this FM: SOTR_SERV_STRING_TO_TABLE

It splits a string into smaller ones with the provided lenght. In your case 1.

data: it_split TYPE TABLE OF string,

       text TYPE string VALUE 'ABCDE'.

CALL FUNCTION 'SOTR_SERV_STRING_TO_TABLE'

     EXPORTING

       text        = text

       line_length = 1

     TABLES

       text_tab    = it_split.

It worked for me.

Regards,

7 REPLIES 7
Read only

former_member188827
Active Contributor
0 Likes
2,154

Try the FM "RSDG_WORD_WRAP". Pass your string to parameter textline and outpulen = 1. Table out_lines will contain desired result.

Also,try the following code:

DATA: str TYPE string VALUE 'ABCDE',ln TYPE i,count TYPE i.

DATA:BEGIN OF wa,

  c1 TYPE char1,

END OF wa.

DATA itab LIKE TABLE OF wa.

ln = strlen( str ).

WHILE count lt ln.

MOVE str+count(1) to wa-c1.

append wa to itab.

CLEAR wa.

count = count + 1.

ENDWHILE.

LOOP AT itab INTO wa.

WRITE:/ wa-c1.

ENDLOOP.


Regards




Read only

Former Member
0 Likes
2,154

Hello Christopher,

lets try this way.

lv_str = 'ABCDE'.

lv_lenght = strlen(lv_str).

lv_pos type i value 0.

lwa_line type line of ITAB.

do lv_length times.

     lwa_line-value = lv_str+lv_pos(1)

     append lwa_line to itab.

     lv_pos = lv_pos+1.

enddo.

Please publish if you face any issue here.

thanks and regards,

Bhaskar

Read only

Former Member
0 Likes
2,154

Hi

The problem is there's no separator element in your string, so you can use SPLIT statament, in your case you need to use a cycle in order to read the string, pick and move the elements into table, just as some guys have suggested you

Max

Read only

Former Member
0 Likes
2,155

Hi Christopher,

Try using this FM: SOTR_SERV_STRING_TO_TABLE

It splits a string into smaller ones with the provided lenght. In your case 1.

data: it_split TYPE TABLE OF string,

       text TYPE string VALUE 'ABCDE'.

CALL FUNCTION 'SOTR_SERV_STRING_TO_TABLE'

     EXPORTING

       text        = text

       line_length = 1

     TABLES

       text_tab    = it_split.

It worked for me.

Regards,

Read only

0 Likes
2,154

Hi Pere,

I found your approach should be the best !

However, when i have to do something with the CString itab, i found it fail to do so.

CALL FUNCTION 'SOTR_SERV_STRING_TO_TABLE'

      EXPORTING

        text        = INPUT_STR

        line_length = 1

      TABLES

        text_tab    = CSTR_ITAB. 'the table could be load here


DATA: v_line type i.

DATA: WA_CSTR like line of CSTR_ITAB.


DESCRIBE TABLE CSTR_ITAB LINES v_line.

LOOP AT CSTR_ITAB into WA_CSTR FROM v_line TO 1. ' doing a reverse loop

'do something here

ENDLOOP.

However, i found it is not possible to get within the loop, anyone can help?

BR - CB

Read only

0 Likes
2,154

Hi Christopher,

I am afraid that it's not possible using this syntax. The first index has to be greater than the second.

LOOP AT CSTR_ITAB into WA_CSTR FROM index1 TO index2


There is a thread talking about looping backwards.


But you could try this code:

DESCRIBE TABLE CSTR_ITAB LINES ind1.


WHILE ind1 >= 1.

    READ TABLE CSTR_ITAB INDEX ind1.


          "do something


     ind1 = ind1 - 1.

ENDWHILE.

Read only

Former Member
0 Likes
2,154

DATA: data TYPE string VALUE 'farid',

       length TYPE i,

       index TYPE i.

TYPES : BEGIN OF ty_tab,

          char TYPE c,

         END OF ty_tab.

DATA : t_tab TYPE TABLE OF ty_tab,

        wa_tab TYPE ty_tab.

length = STRLEN( data ).

WHILE index < length.

   wa_tab-char = data+index(1).

   APPEND wa_tab to t_tab.

  add 1 to index.

ENDWHILE.