SAP has this simple short and sweet function module called STRING_CENTER which does what it's name implies. Center a string, align in the middle. However: the function module is marked as obsolete and I can't find it's replacement/successor.. So here's the coding.
CLASS-METHODS:
string_center changing str type any.
The method implemented:
METHOD string_center.
DATA: lv_len type SY-FDPOS,
lv_len2 type SY-FDPOS,
lv_str type string.
FIELD-SYMBOLS: <fs> type any.
lv_str = str.
lv_len = STRLEN( str ).
move space to str.
IF lv_len > 0.
DESCRIBE FIELD str LENGTH lv_len2 in character mode.
lv_len2 = ( lv_len2 - lv_len ) / 2.
ASSIGN str+lv_len2(lv_len) TO <fs>.
MOVE lv_str TO <fs>.
ENDIF.
ENDMETHOD.
Quite an ingenious bit of coding really... Thanks SAP !
