The REPLACE instruction is an instruction to search for a specific character and replace it if it is included.
Sample Code：Replace string
DATA: V_TEXT TYPE String. V_TEXT = 'AAAAFINDCBBBFINDB'. REPLACE 'FIND' IN V_TEXT WITH 'XX'. IF SY-SUBRC = 0. WRITE V_TEXT. "AAAAXXCBBBFINDB ENIDF.
In this example, the REPLACE command is used to replace the variable “V_TEXT” with the character “FIND” replaced by “XX”. System variable: SY-SUBRC returns ‘0’ if it can be found, and ‘4’ if it cannot be found. Describe each process after branching with IF statement etc. I can do things.
In this case, there are two’FIND’s, but only the first’FIND’ is replaced, so the result is “AAAAXXCBBBBFINDB”. See below for an example of replacing all characters.
Sample Code：Replace all characters
DATA: V_TEXT TYPE String. V_TEXT = 'AAAAFINDCBBBFINDB'. REPLACE ALL OCCURRENCES OF 'FIND' IN V_TEXT WITH 'XX'. IF SY-SUBRC = 0. WRITE V_TEXT. "AAAAXXCBBBXXB ENIDF.
Similar to the first example, this example uses the REPLACE instruction to replace the variable “V_TEXT” with the character “FIND” replaced by “XX”.
The difference is that only the first character is replaced or all characters are replaced. If you want to replace all characters, you need the option ALL OCCURRENCES OF. When replacing the first character, the description will be FIRST OCCURRENCES OF, but as the default is FIRST OCCURRENCES OF, the description will often be omitted.
In this case, all the’FIND’ are replaced, so the result “AAAAXXCBBBBXXB” is returned.
REPLACE instruction summary
Keep in mind that you can use it when you want to replace a character or when you want to erase a character.
This is useful for replacing a specific character with another character, and for removing the digit (,) separator for amounts and quantities.