Application Development 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: 

Problem with Replace statement?

Former Member
0 Kudos

Hi Guys,

I am using Replace statement for replacing '%20' in a partucular field like given below

" Replace All occurrences of REGEX '%20' in I_ZSTR_BPSITEUSER-FIRSTNAME with space ."

But the problem is if Firstname = " Gopi%20%20%20A " after using this replace i am getting as

" GopiA " but what my req is i need like "Gopi A"(i want spaces in %20 place)so how to get it?

Thanks,

Gopi

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

Try this..

DATA: patt TYPE string VALUE '%20',
      text TYPE char100 VALUE 'Gopi%20%20%20A'.

DATA: result_tab TYPE match_result_tab.
DATA: v_length TYPE i.

FIELD-SYMBOLS <match> LIKE LINE OF result_tab.

* Find
FIND ALL OCCURRENCES OF patt IN
     text
     RESULTS result_tab.

SORT result_tab BY offset DESCENDING.

* Replace
LOOP AT result_tab ASSIGNING <match>.

  v_length = <match>-length - 1.
  text+<match>-offset(v_length) = space.

  SHIFT text+<match>-offset LEFT DELETING LEADING space.

  text+<match>-offset(1) = space.

ENDLOOP.

WRITE: / text.

Thanks

Naren

5 REPLIES 5

Former Member
0 Kudos

Hi,

I am sorry I am not sure how to use the regular expressions..

Please try this..it worked..

DATA: patt TYPE string VALUE '%20',
      text TYPE char100 VALUE 'Gopi%20%20%20A'.

DATA: result_tab TYPE match_result_tab.

FIELD-SYMBOLS <match> LIKE LINE OF result_tab.

* Find
FIND ALL OCCURRENCES OF patt IN
     text
     RESULTS result_tab.

* Replace
LOOP AT result_tab ASSIGNING <match>.
  text+<match>-offset(<match>-length) = space.
ENDLOOP.

WRITE: / text.

Thanks

Naren

0 Kudos

Hi Naren,

Thanks for ur reply.But still small probs if u enter %20 i want only single space not multiple spaces ok with ur logic i am getting more spaces for each %20 like more than one can u make it just one space for each %20.

Thanks,

Gopi.

0 Kudos

This is not pretty but works.


DATA: string(80)  TYPE c VALUE 'Reich%20%20%20M',
      string2(80) TYPE c,
      string3(80) TYPE c,
      search(3)   TYPE c VALUE '%20',
      continue(1) TYPE c VALUE 'Y',
      i_off       TYPE i.

WHILE continue = 'Y'.
  REPLACE FIRST OCCURRENCE OF search IN string WITH space.

  FIND search IN string MATCH OFFSET i_off.

  IF string+i_off(6) = search.
    continue = 'Y'.
  ELSE.
    SPLIT string AT search INTO string2 string3.
    SHIFT string3 LEFT DELETING LEADING search.
    continue = 'N'.
  ENDIF.
ENDWHILE.

CLEAR string.
CONCATENATE string2 string3 INTO string SEPARATED BY space.

WRITE: string.

0 Kudos

Try this.

w_data = 'Gopi%20%20%20A'.

do.

replace '%20' with space into w_data.

if sy-subrc ne 0.

exit.

endif.

enddo.

write: / w_data.

Thanks,

Mahesh

Former Member
0 Kudos

Hi,

Try this..

DATA: patt TYPE string VALUE '%20',
      text TYPE char100 VALUE 'Gopi%20%20%20A'.

DATA: result_tab TYPE match_result_tab.
DATA: v_length TYPE i.

FIELD-SYMBOLS <match> LIKE LINE OF result_tab.

* Find
FIND ALL OCCURRENCES OF patt IN
     text
     RESULTS result_tab.

SORT result_tab BY offset DESCENDING.

* Replace
LOOP AT result_tab ASSIGNING <match>.

  v_length = <match>-length - 1.
  text+<match>-offset(v_length) = space.

  SHIFT text+<match>-offset LEFT DELETING LEADING space.

  text+<match>-offset(1) = space.

ENDLOOP.

WRITE: / text.

Thanks

Naren