Often when developing ABAP programs, it becomes necessary to replace a substring with another or find string length ABAP. For example, replace a comma with a dot or insert a space instead of a separator and many other options. The ABAP REPLACE character 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 call options, we will look at each of them in more detail:
REPLACE ABAP with offset (off) and substring length (len).
This option replaces the substring we select using the off offset and length specified in the len variable with the value specified in the with argument. At least one of these arguments must be specified with this call option. Examples:
1 2 |
replace( val = |abap-blog.com/contacts| off = 14 len = 8 with = 'abap' ). " = abap-blog.com/abap |
REPLACE using like add.
If we omit the len parameter or set it to len = 0, then this construct will work like an insert.
1 2 3 4 |
replace( val = |abap-blog/abap| off = 9 len = 0 with = '.com' ). " = abap-blog.com/abap replace( val = |abap-blog/abap| off = 9 with = '.com' ). " = abap-blog.com/abap |
REPLACE substrings of length LEN from beginning
If only the len parameter is specified, which is the same as specifying off = 0, then the first element of length len will be replaced.
1 2 3 4 |
replace( val = |abap-blog/abap| off = 0 len = 9 with = 'abap-blog.com' ). " = abap-blog.com/abap replace( val = |abap-blog/abap| len = 9 with = 'youcoder.ru' ). " = abap-blog.com/abap |
Inserting at the end of string using REPLACE ABAP.
If the value of the off parameter is equal to the length of the string, then the value of the with parameter will be inserted at the end of the string:
1 2 3 4 |
replace( val = |abap-blog.com| off = 13 len = 0 with = '/abap' ). " = abap-blog.com/abap replace( val = |abap-blog.com| off = 13 with = '/abap' ). " = abap-blog.com/abap |