Srting splitting ABAP SUBSTRING

There are many options for breaking a string into parts, depending on the task.
There are tasks in which there is a need to select a substring from a string according to certain criteria. To solve this problem, the substring function with many of its options is excellent. Let us consider each of them in more detail:

SUBSTRING

Function SUBSTRING returns us a substring of length equal to len while taking this string indented off.

lv_substring = substring( val = 'ABCDEFGH' off = 2 len = 2 ). " CD

In our case, the result of the function will be: CD

SUBSTRING_FROM

Returns a substring from the first occurrence of sub characters including these characters to the end of the string.
By the result we get: CDEFGH

lv_substring = substring_from( val = 'ABCDEFGH' sub = 'CD' ) " CDEFGH

SUBSTRING_AFTER

Returns a substring from the first occurrence of sub characters NOT including these characters to the end of the string.
By the result we get: EFGH

lv_substring = substring_after( val = 'ABCDEFGH' sub = 'CD' ). " EFGH " CDEFGH

SUBSTRING_BEFORE

Returns a substring from the first character of the string to the first occurrence of sub characters NOT including these characters.
By the result we get: AB

lv_substring = substring_before( val = 'ABCDEFGH' sub = 'CD' ). " AB

SUBSTRING_TO

Returns a substring from the first character of the string to the first occurrence of sub characters including these characters.
By the result we get: ABCD

lv_substring = substring_to( val = 'ABCDEFGH' sub = 'CD' ). " ABCD

SPLIT

There are also tasks of splitting one line into several substrings.
Splitting a string with the SPLIT operator by the specified delimiter

DATA(lv_dom_http) = 'https://youcoder.ru'. SPLITlv_img_httpAT'://'INTO:DATA(lv_http)DATA(lv_dom_http).

According to the result of the program, the following values will be assigned to the variables:

lv_http = 'https'. lv_dom_http = 'youcoder.ru'.

Patterns how to split a string into several substrings of a certain length.

An example of real use may be as follows. It often happens that a standard method or FM does not accept a string as an input, but a table with sequences of characters of a certain length. An example of using the construction in a real problem, and its solutions can be found in the article: Text output in a window, as well as this is discussed in detail in the video for this article.

Pattern using SUBSTRING:

DATA:
  lt_text_table TYPE STANDARD TABLE OF text120.
  
" lv_text - this is an arbitrary string that we break

" Get the string length
DATA(lv_strlen) = strlen( lv_text ). 

" Split substring length
DATA(lv_sep) = 120.
  
DATA(lv_count) = 0.
" While the length of the remainder of the substring is less than the length of the partition
WHILE lv_strlen - lv_count > lv_sep.

  " We select a substring with lv_count of length lv_sep. 
  DATA(lv_substr) = CONV ( substring( val = lv_text off = lv_count len = lv_sep ) ).
  
  " Add a substring to the table for future use.
  APPEND lv_substr TO lt_text_table.
  
  " Add the length of the cut line to the counter.
  lv_count = lv_count + lv_sep.
ENDWHILE.

" We do the same thing, but for the last part of the line
lv_substr = substring( val = lv_text off = lv_count ).
APPEND lv_substr TO lt_text_table. 

A short pattern for getting ABAP substrings of a certain length:

DATA:
  lt_text_table TYPE STANDARD TABLE OF text40.

" lv_text - this is an arbitrary string that we break

DATA(lv_strlen) = strlen( lv_text ).
DATA(lv_sep) = 40.
DATA(lv_count) = 0.

WHILE lv_strlen - lv_count > lv_sep.
  APPEND lv_text+lv_count(lv_sep) TO lt_text_table.
  lv_count = lv_count + lv_sep.
ENDWHILE.

APPEND lv_text+lv_count TO lt_text_table. 
FM template HR_RU_SLPIT_STRING0:

The use of this FM is shown by filling in the name fields when creating a lender.

l_string=' "YOUCODER" loaded from an external system'.
CALLFUNCTION'HR_RU_SLPIT_STRING0'
	EXPORTING
	  string=l_string
	  length=40
	  separator=l_separator
	TABLES
	  split=lt_split.	
LOOPATlt_splitREFERENCEINTODATA(lr_split).
	CASEsy-tabix.
	  WHEN1.
		ls_data-name=lr_split->char255.
	  WHEN2.
		ls_data-name_2=lr_split->char255.
	  WHEN3.
		ls_data-name_3=lr_split->char255.
	  WHEN4.
		ls_data-name_4=lr_split->char255.
	ENDCASE.
ENDLOOP.