Created
June 23, 2022 06:41
-
-
Save alezhu/89443a2f3ab1e9015b3bbe12c49a8356 to your computer and use it in GitHub Desktop.
Split string into string table by max length but only on words borders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CLASS zcl_tm_327_str_utils DEFINITION PUBLIC CREATE PUBLIC . | |
PUBLIC SECTION. | |
METHODS split_string_by_space_max_len | |
IMPORTING | |
!iv_value TYPE csequence | |
!iv_max_len TYPE i | |
!iv_max_rows TYPE i DEFAULT 0 | |
RETURNING | |
VALUE(rt_result) TYPE string_table . | |
ENDCLASS. | |
CLASS ZCL_STR_UTILS IMPLEMENTATION. | |
METHOD split_string_by_space_max_len. | |
CHECK iv_value IS NOT INITIAL. | |
SPLIT iv_value AT space INTO TABLE DATA(lt_words). | |
DATA(lv_from) = 1. | |
DO. | |
DATA(lv_capacity) = iv_max_len + 1. | |
DATA(lt_row) = VALUE string_table( ). | |
LOOP AT lt_words ASSIGNING FIELD-SYMBOL(<v_str>) FROM lv_from. | |
DATA(lv_len) = strlen( <v_str> ) + 1. | |
IF lv_capacity - lv_len < 0. | |
EXIT. | |
ENDIF. | |
APPEND <v_str> TO lt_row. | |
SUBTRACT lv_len FROM lv_capacity. | |
ADD 1 TO lv_from. | |
IF lv_capacity = 0. | |
EXIT. | |
ENDIF. | |
ENDLOOP. | |
IF sy-subrc NE 0. | |
EXIT. | |
ENDIF. | |
APPEND concat_lines_of( table = lt_row sep = ` ` ) TO rt_result. | |
IF iv_max_rows IS NOT INITIAL AND lines( rt_result ) >= iv_max_rows. | |
EXIT. | |
ENDIF. | |
ENDDO. | |
ENDMETHOD. | |
ENDCLASS. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment