Replacing characters in a string ABAP - REPLACE

Often when developing ABAP programs, it becomes necessary to replace the substring with another. The replacement function will help. This function replaces a substring of text with a string of characters and returns the modified text as a result. There are two options for calling, we will consider each of them in more detail:

Replace part using offset (off) and substring length after offset(len).

This option replaces the substring that we select using the off offset and length specified in the len variable with the value specified in the with argument. Using this call option, at least one of these arguments must be defined. Examples:

replace(val=|youcoder.ru/contacts.html|off=12len=8with='abap').
" = youcoder.ru/abap.html

If only the off parameter is specified, which is similar to the len = 0 parameter, then this construct will work as an insert.

replace(val=|youcoder/abap.html|off=8len=0with='.ru').
" = youcoder.ru/abap.html

replace(val=|youcoder/abap.html|off=8with='.ru').
" = youcoder.ru/abap.html

If only the len parameter is specified, which is similar to the indication off = 0, then the first element of the length len will be replaced.

replace(val=|youcoder/abap.html|off=0len=8with='youcoder.ru').
" = youcoder.ru/abap.html

replace(val=|youcoder/abap.html|len=8with='youcoder.ru').
" = youcoder.ru/abap.html

If the value of the off parameter is equal to the length of the line, then the value of the with parameter will be inserted at the end of the line:

replace(val=|youcoder.ru|off=11len=0with='/abap.html').
" = youcoder.ru/abap.html

replace(val=|youcoder.ru|off=11with='/abap.html').
" = youcoder.ru/abap.html

Replace using a substring (sub) or a regular expression (regex).

This definition parameter searches for a certain number of occurrences of occ in a string, checking whether the substring specified in substring matches the regular expression specified in the regex argument. And replaces these occurrences with the value passed in with. If the value of the with parameter has a fixed length, then the spaces indicated at the end will be ignored by the program. If you pass the value 0 to the occ argument, all occurrences will be replaced. In addition, by default, substitution values ??are case-sensitive, but this can be changed using the case parameter.

If the occ parameter is not specified, then by default it will be occ = 1, and only the first occurrence of the substring will be replaced:

replace(val=|yc/index.html,yc/abap.html|sub='yc'with='youcoder.ru').
" = youcoder.ru/index.html,yc/abap.html

The default value for the parameter is case = abap_true, which means case-sensitive. That is, specifying case = abap_true or skipping this parameter, you will get the same result:

replace(val=|YC/index.html,yc/|sub='yc'with='youcoder.ru'occ=0).

replace(val=|YC/index.html,yc/|sub='yc'with='youcoder.ru'case=abap_trueocc=0).
" = youcoder.ru/index.html,youcoder.ru/

For the replacement to work correctly, you must specify the case = abap_false parameter so that the case is ignored.

replace(val=|YC/index.html,yc/|sub='yc'with='youcoder.ru'case=abap_falseocc=0).
" = youcoder.ru/index.html,youcoder.ru/

An example from a real task.

When using comparison masks for the range table and to limit the query, you must pass the value in a different way. In this case, this replacement is conveniently done using replace with the parameter regex, to which we pass the character that needs to be replaced:

replace(val='*TRUMPET*'regex='\*'with='\%'occ=0).
" = %TRUMPET%

As a result, we get a value that can be specified to limit the request in the supplement WHERE. For instance: WHERE name LIKE lv_str.