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

Alternative for REPLACE AT ALL OCCURANCES

Former Member
0 Likes
2,691

Hi friends,

I am facing a issue where i need to replace " with "" at all occurance in a string.

My server is not supporting the Statement REPLACE AT ALL OCCURANCES statement.

We tried with Replace statement as it is replacing the first occurance of " in the word. Ineed to replace every occurance.

Example : if the string is "My""Name"

Then expected out put is """My""""Name"""

Help will be appreciated...

Please help me in this regard...

Regards,

aanand

1 ACCEPTED SOLUTION
Read only

RaymondGiuseppi
Active Contributor
0 Likes
2,164

You may try to combine TRANSLATE and REPLACE to do the job

- TRANSLATE " to a character, e.g. # (USING ''#) , you are sure is not in the string

- DO/REPLACE # WITH ""/ENDDO until SY-SUBRC is not zero

Regards

Hi friends,

I am facing a issue where i need to replace " with "" at all occurance in a string.

My server is not supporting the Statement REPLACE AT ALL OCCURANCES statement.

We tried with Replace statement as it is replacing the first occurance of " in the word. Ineed to replace every occurance.

Example : if the string is "My""Name"

Then expected out put is """My""""Name"""

Help will be appreciated...

Please help me in this regard...

Regards,

aanand

11 REPLIES 11
Read only

Former Member
0 Likes
2,164

hiiii

it is working..

use like below

DATA : w_a(30)      TYPE c VALUE   '01&34&6abcd',
            w_rule(30) TYPE c value 'X',
            w_string1(50) TYPE c.

w_string1 = w_a.
replace all occurrences of '&' in w_string1 with w_rule.

write: w_string1.

regards

twinkal

Read only

matt
Active Contributor
0 Likes
2,164

Do you mean you are on an earlier version that doesn't support REPLACE ALL OCCURENCES?

You could REPLACE to something other than ", for example §. Then go through the string again, replacing § with "".

E.g.

"text" goes to §text§ goes to ""text""

You have to put the REPLACE in a loop to get all occurences.

matt

Read only

Former Member
0 Likes
2,164

Are you checking on the first character of the string? You could do a check using LIKE

l_string type C

l_st1 type l_string

l_st2 type l_string

l_var type C VALUE '"'

If l_string(1) like l_var.

Split l_string AT <where needs split> into l_st1 l_st2.

Concatenate l_st1 ' ' l_st2 into l_string.

Endif.

You can alson concatentate variables and separate by SPACE or SEP. Sep will place a separator between each variable. Hope this helps or gives ya some direction to go with.

Regards,

C

Read only

Former Member
0 Likes
2,164

Thank you for replying...

but my server version is not supporting

"replace all occurrences of " statement...

Read only

matt
Active Contributor
0 Likes
2,164

You do realise I'm suggesting using the old form of replace:

REPLACE sub_string WITH new INTO dobj 
        [IN {BYTE|CHARACTER} MODE] 
        [LENGTH len]. 

NOT replace all occurences...

matt

Read only

valter_oliveira
Active Contributor
0 Likes
2,164

Hello.

Try this code:


DATA: var1 TYPE string VALUE '"My""Name"',
      var2 TYPE string,
      lenght TYPE i.

DATA: pos TYPE i.
CLEAR pos.
lenght = STRLEN( var1 ).

DO lenght TIMES.
  IF var1+pos(1) EQ '"'.
    CONCATENATE var2 var1+pos(1) var1+pos(1) INTO var2.
  ELSE.
    CONCATENATE var2 var1+pos(1) INTO var2.
  ENDIF.
  ADD 1 TO pos.
ENDDO.

WRITE var2.

Regards.

Valter Oliveira.

Read only

RaymondGiuseppi
Active Contributor
0 Likes
2,165

You may try to combine TRANSLATE and REPLACE to do the job

- TRANSLATE " to a character, e.g. # (USING ''#) , you are sure is not in the string

- DO/REPLACE # WITH ""/ENDDO until SY-SUBRC is not zero

Regards

Read only

0 Likes
2,164

>

> You may try to combine TRANSLATE and REPLACE to do the job

> - TRANSLATE " to a character, e.g. # (USING ''#) , you are sure is not in the string

> - DO/REPLACE # WITH ""/ENDDO until SY-SUBRC is not zero

> Regards

I should have mentioned that - it's the solution I used to use before REPLACE got updated. Nice one.

Read only

Former Member
0 Likes
2,164

check this

REPORT  z_sdn.

data text(30).

text = '"My""Name"'.


replace all occurrences of '"' in text with '""'.

write text.

Read only

Former Member
0 Likes
2,164

This message was moderated.

Read only

Former Member
0 Likes
2,164

Closed