Hi Amine,
You analysis was good, need to use proper ABAP code , once you maintained required logic, do syntax check and run it for test case.
Pls check about replace stmt:
http://help.sap.com/SAPHELP_470/Helpdata/EN/fc/eb3398358411d1829f0000e829fbfe/content.htm
Thanks