String wrap

SAP provides the function module ‘RKD_WORD_WRAP’ to wrap character type variables. However, this FM cannot be used to wrap string type variables directly. If you need to work with string wrap functionality without converting the string type data into character type variables, you can either transfer the string type data into a character type variable and use this FM or implement your own code to handle string wrapping. Alternatively, you can use the function module below or copy the code snippet provided to apply the wrapping logic for strings.

FUNCTION z_string_wrap.
*”———————————————————————-
*”*”Local Interface:
*” IMPORTING
*” VALUE(TEXTLINE) TYPE STRING
*” VALUE(OUTPUTLEN) TYPE I DEFAULT 50
*” TABLES
*” OUT_LINES
*”———————————————————————-
DATA:
lk_line TYPE string,
lv_offset TYPE i,
lv_to_pos TYPE i,
lv_new_target_pos TYPE i,
lv_length TYPE i.

IF outputlen IS INITIAL.
outputlen = ’50’.
ENDIF.
* Get string length
lv_length = STRLEN( textline ).

* If the string length is less than output length requested
IF lv_length LE outputlen.

* Write the string value
lk_line = textline.

* Append the wrapped string
APPEND lk_line TO out_lines[].

* If greater than max characters
ELSE.

* Get the to postion. Default is 50
lv_to_pos = outputlen.

* Breaking the string to fields of max reqested characters
DO.

* Get data to the length
lk_line = textline+lv_offset(lv_to_pos).

* Append the wrapped string
APPEND lk_line TO out_lines[].

* Increasing the offset value
lv_offset = lv_offset + lv_to_pos.

* If offset is equal to the length of the string
IF lv_offset = lv_length.

* Exit from the do-end loop
EXIT.

ENDIF.

* New position till where the string has been written/split
lv_new_target_pos = lv_offset + outputlen.

* If new position is greater than the string length
IF lv_new_target_pos > lv_length.

* The position till where we need to read is this subtraction
* This would be less than OUTPUTLEN
lv_to_pos = lv_length – lv_offset.

ELSE.
* Else just read the OUTPUTLEN value
lv_to_pos = outputlen.
ENDIF.

ENDDO.

ENDIF.

ENDFUNCTION.

To get such pragmatic issues and goals directly to your inbox, kindly Buy in. We regard your security and view safeguarding it in a serious way.

If you enjoyed this post, please hit the share button on the left side of your screen. Additionally, if you’re interested in learning more about string wrap techniques and their applications, stay tuned for upcoming content.

Many thanks for your time!!

 

YOU MAY LIKE THIS

ABAP on SAP HANA. Part III. Debugging in ADT

SAP OData v4: Unleash the Power of Efficient Data Access

Chart of accounts in SAP

 

X
WhatsApp WhatsApp us