2011 Jun 17 2:55 AM
Hi All,
I'm a little new to ABAP so bear with me. In an ABAP class, I have a string which I currently need to remove both single and double quotes from. I am currently solving this with two separate REPLACE statements.
* Remove all occurrences of single and double quotes
REPLACE ALL OCCURRENCES OF SUBSTRING '''' IN lv_all_params WITH ''.
REPLACE ALL OCCURRENCES OF SUBSTRING '"' IN lv_all_params WITH ''.
I have two questions regarding this:
1) Is there a more efficient way of solving this rather than with 2 separate REPLACE statements because if more characters also need to be removed, then I have to keep adding REPLACE statements.
2) Is there a way that if more characters are found that need to be removed, it doesn't have to result in code changes? That is, I can configure the list of characters that need replacing outside of the code so that code remains unchanged.
Thanks very much in advance.
2011 Jun 17 5:24 AM
Try TRANSLATE statement like below.
TRANSLATE lv_all_params USING '" '' '. " Single line can be used to replace more characters
Also check on REGULAR EXPRESSION,
http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/902ce392-dfce-2d10-4ba9-b4f777843182
2011 Jun 17 5:24 AM
Try TRANSLATE statement like below.
TRANSLATE lv_all_params USING '" '' '. " Single line can be used to replace more characters
Also check on REGULAR EXPRESSION,
http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/902ce392-dfce-2d10-4ba9-b4f777843182
2011 Jun 17 5:28 AM
Hi ,
Try using REGEX, as shown in the link below from sap abapp
[http://help.sap.com/abapdocu_702/en/abenregex_replace.htm]
2011 Jun 17 5:44 AM
Translate text using 'pattern' .
'pattern' is considered to be pairs of charecters. first charecter of each pair will be converted to second charecter of the same pair.
Example:
Converts the characters "A" to "B", "a" to "b", and vice versa. text contains "Abracadabra" after the conversion.
DATA text TYPE string.
text = `Barbcbdbarb`.
TRANSLATE text USING 'ABBAabba'.
here pattern 'ABBAabba' - can be interrpretted as 'A-B B-A a-b b-a' .
2011 Jun 17 5:41 AM
Hi,
i suggest what sap fan told do that. Use translate.
please check the link also.
http://help.sap.com/saphelp_nw70/helpdata/en/fc/eb33a5358411d1829f0000e829fbfe/content.htm
Regards,
Dhina..
Edited by: Dhina DMD on Jun 17, 2011 6:42 AM
2011 Jun 17 6:57 AM
Hi Joy,
You can use regular expression.
DATA : lv_regex TYPE string VALUE '[''"]',
lv_str TYPE string VALUE '""''text''""'.
REPLACE ALL OCCURRENCES OF REGEX lv_regex IN lv_str WITH ''.
WRITE lv_str.
Regards,
Marvin
2011 Jun 17 8:07 AM
Hi Marvin,
Thanks very much. I have decided to use regular expressions but just had to modify your code cause it didn't work as expected. Had to do the data as this and all works as expected:
DATA: lv_regex TYPE string VALUE '''|\"'.
Now, if anyone has any suggestions for Question number 2 in my original post else I'm just going with this solution only.
2022 Jul 15 2:18 AM
"This is not actually allowed, so using as my spacer to replace later
ELSE.
EXIT.ENDMETHOD.best way i have found: