Split ABAP string to internal table retaining whole words
Here sample code which splits ABAP string to internal table.
It retains whole words and doesn't break them between lines.
This important if your string contains condition, which should be used in dynamic WHERE statement.
It retains whole words and doesn't break them between lines.
This important if your string contains condition, which should be used in dynamic WHERE statement.
CONSTANTS:
co_max TYPE i VALUE 15.
DATA:
lv_string TYPE string,
lv_sum TYPE i,
lv_txt1(co_max) TYPE c,
lt_txt1 LIKE TABLE OF lv_txt1,
lv_len1 TYPE i,
lv_txt2(co_max) TYPE c,
lt_txt2 LIKE TABLE OF lv_txt2,
lv_len2 TYPE i.
lv_string = 'WHERE BNAME = ''SAP*'' AND NRPRO = ''001'''.
SPLIT lv_string AT ' ' INTO TABLE lt_txt1.
LOOP AT lt_txt1 INTO lv_txt1.
lv_len1 = strlen( lv_txt1 ).
ASSERT lv_len1 <= co_max.
lv_len2 = strlen( lv_txt2 ).
lv_sum = lv_len1 + lv_len2 + 1.
IF lv_sum <= co_max.
CONCATENATE lv_txt2 lv_txt1 INTO lv_txt2 SEPARATED BY ' '.
CONDENSE lv_txt2.
ELSE.
APPEND lv_txt2 TO lt_txt2.
lv_txt2 = lv_txt1.
ENDIF.
ENDLOOP.
APPEND lv_txt2 TO lt_txt2.
LOOP AT lt_txt2 INTO lv_txt2.
WRITE: / lv_txt2.
ENDLOOP.
* Output:
*
* WHERE BNAME =
* 'SAP*' AND
* NRPRO = '001'
co_max TYPE i VALUE 15.
DATA:
lv_string TYPE string,
lv_sum TYPE i,
lv_txt1(co_max) TYPE c,
lt_txt1 LIKE TABLE OF lv_txt1,
lv_len1 TYPE i,
lv_txt2(co_max) TYPE c,
lt_txt2 LIKE TABLE OF lv_txt2,
lv_len2 TYPE i.
lv_string = 'WHERE BNAME = ''SAP*'' AND NRPRO = ''001'''.
SPLIT lv_string AT ' ' INTO TABLE lt_txt1.
LOOP AT lt_txt1 INTO lv_txt1.
lv_len1 = strlen( lv_txt1 ).
ASSERT lv_len1 <= co_max.
lv_len2 = strlen( lv_txt2 ).
lv_sum = lv_len1 + lv_len2 + 1.
IF lv_sum <= co_max.
CONCATENATE lv_txt2 lv_txt1 INTO lv_txt2 SEPARATED BY ' '.
CONDENSE lv_txt2.
ELSE.
APPEND lv_txt2 TO lt_txt2.
lv_txt2 = lv_txt1.
ENDIF.
ENDLOOP.
APPEND lv_txt2 TO lt_txt2.
LOOP AT lt_txt2 INTO lv_txt2.
WRITE: / lv_txt2.
ENDLOOP.
* Output:
*
* WHERE BNAME =
* 'SAP*' AND
* NRPRO = '001'
See other related notes in my infodepot:
Full list of examples in my infodepot
If you have a question, have found an error or just want to contact me, please use this form.
If you have a question, have found an error or just want to contact me, please use this form.
Copyright (C) 2011 http://www.kerum.pl/infodepot/
Disclaimer: I am not affiliated or related to any division or subsidiary of SAP AG.
Trademarks or registered trademarks of any products or companies referred to on this site belong to those companies.
Anyone using the given solutions, is doing it under his/her own responsibility and at own risk.