Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Removing specified characters from string

Former Member
0 Likes
112,782

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.

1 ACCEPTED SOLUTION
Read only

awin_prabhu
Active Contributor
0 Likes
45,476

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

7 REPLIES 7
Read only

awin_prabhu
Active Contributor
0 Likes
45,477

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

Read only

Former Member
0 Likes
45,476

Hi ,

Try using REGEX, as shown in the link below from sap abapp

[http://help.sap.com/abapdocu_702/en/abenregex_replace.htm]

Read only

0 Likes
45,476

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' .

Read only

Former Member
0 Likes
45,476

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

Read only

Former Member
45,476

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

Read only

0 Likes
45,476

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.

Read only

0 Likes
45,476
METHOD remove_special_chars .
DATA:
lv_allowed_char_m340(100) TYPE c VALUE ' &,-.0123456789:;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_/()~'.

FIELD-SYMBOLS: <fs_cell> TYPE simple.
LOOP AT tab ASSIGNING FIELD-SYMBOL(<fs_hdr>).
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <fs_hdr> TO <fs_cell>.
IF sy-subrc NE 0. EXIT. ENDIF.

DO.
IF <fs_cell> CN lv_allowed_char_m340.
<fs_cell>+sy-fdpos(1) = '~'.

"This is not actually allowed, so using as my spacer to replace later

ELSE.

EXIT.
ENDIF.
ENDDO.

REPLACE ALL OCCURRENCES OF '~' IN <fs_cell> WITH ''.
ENDDO.
ENDLOOP.

ENDMETHOD.best way i have found: