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:

" =

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

" =

" =

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.

" =

" =

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 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:

" =,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:


" =,

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

" =,

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:


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.