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 String in itab by multiple arguments

0 Kudos

Hi,

I have a table with the bellow entries:

12;10;11.9-8    aaa    bbbb

12                    aaa     cccc   

I need to split this entry to 3 columns:

12    aaa   bbbb

10    aaa   bbbb

11    aaa   bbbb

9     aaa   bbbb

8     aaa   bbbb

12   aaa   cccc

It is like a split xy at ';' into table, but I have multiple separators.

Do you have an idea how to solve it?

 

Thanks for support!

3 REPLIES 3

Sandra_Rossi
Active Contributor

Split at ';' then split at '.' then split at '-'.

raymond_giuseppi
Active Contributor

Try to replace all occurences of separators by the same value, then split at this value.

 

SPLIT replace( val  = text
               regex = `[;,.]`
               with = `-`
               occ  = 0 )  AT `-` INTO TABLE itab.

 

MarcoK
Explorer

My first attempt for a solution was a bit complicated but taking into account the note from @raymond_giuseppi it's quite easy:

TYPES: BEGIN OF lty_result_tab,
         col1 TYPE string,
         col2 TYPE string,
         col3 TYPE string,
       END OF lty_result_tab.

DATA lt_result_tab TYPE STANDARD TABLE OF lty_result_tab WITH DEFAULT KEY.
CONSTANTS lc_col_separator TYPE c LENGTH 1 VALUE space.

" Testdata
DATA lt_data TYPE STANDARD TABLE OF text100 WITH DEFAULT KEY.
lt_data = VALUE #( ( '12;10;11.9-8    aaa    bbbb' )
                   ( '12                    aaa     cccc' ) ).

LOOP AT lt_data ASSIGNING FIELD-SYMBOL(<ls_data>).
  SPLIT condense( <ls_data> ) AT lc_col_separator INTO DATA(lv_col1_value) 
  DATA(lv_col2_value) DATA(lv_col3_value).
  SPLIT replace( val   = lv_col1_value
                 regex = `[;,.]`
                 with  = `-`
                 occ   = 0 ) AT `-` INTO TABLE DATA(lt_col1_splitted).

  lt_result_tab = VALUE #( BASE lt_result_tab FOR lv_splitted_value IN lt_col1_splitted
                           ( col1 = lv_splitted_value col2 = lv_col2_value col3 = lv_col3_value ) ).

ENDLOOP.