Skip to Content
author's profile photo Former Member
Former Member

Replace Command

Friends

My requirement is to "Replace all occurrences of a particular word in a string with an other word".

Is there no "Replace all Occurrences" command in ABAP ?

I checked in the help and could not find it.

Is there any direct way of doing it ?

I can split the string into an internal table and use the replace. However, I am just curious to know if there is any other short cut ?

Here is my sample program I tested.

data: lf_str type string.

lf_str = 'This is a This test This is a This'.

write: / lf_str.

*Replace All Occurrences Of 'This' in lf_str with 'That'.

Replace 'This' with 'That' into lf_str.

write: / lf_str.

Thanks

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Posted on Feb 22, 2005 at 04:42 AM

    Yes it is available.

    following are options available with <i><b>replace</b></i>

    REPLACE [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF] 
            [SUBSTRING] sub_string 
            IN [ section_of] dobj WITH new 
            [IN {BYTE|CHARACTER} MODE] 
            [{RESPECTING|IGNORING} CASE] 
            [REPLACEMENT COUNT rcnt] 
            [REPLACEMENT OFFSET roff] 
            [REPLACEMENT LENGTH rlen]. 
    

    Sample code taken from ABAP key word documentation.

    DATA: text1 TYPE string, 
          text2(18) TYPE c, 
          cnt TYPE i, 
          off TYPE i, 
          len TYPE i. 
    
    text1 = text2 = 'I know you know'. 
    
    REPLACE ALL OCCURRENCES OF 'know' IN: 
            text1 WITH 'should know that' 
                  REPLACEMENT COUNT  cnt 
                  REPLACEMENT OFFSET off 
                  REPLACEMENT LENGTH len, 
            text2 WITH 'should know that' 
                  REPLACEMENT COUNT  cnt 
                  REPLACEMENT OFFSET off 
                  REPLACEMENT LENGTH len. 
    

    Regards

    Raja

    Add a comment
    10|10000 characters needed characters exceeded

    • Hi Sundara

      The additions Durairaj mentioned are introduced with the version 4.7. Hence, if your version is below this, you won't have those options. Nevertheless, you can use <b>REPLACE</b> statement within a <b>"DO...ENDDO"</b> loop .

      <u>E.g.</u>

      DO .
        REPLACE <a> IN <str> WITH <b> .
        IF sy-subrc ne 0 .
          EXIT .
        ENDIF .
      ENDDO .

      Or you can use <b>TRANSLATE</b> statement.

      <u>E.g.</u>

      TRANSLATE <str> USING 'ab' . 
      *changes all 'a's to 'b's

      Hope this helps...

      Regards

      *--Serdar

      ssimsekler@yahoo.com

  • author's profile photo Former Member
    Former Member
    Posted on Feb 22, 2005 at 10:36 AM

    Hi everyone,

    you could also use function module 'REPLACE_STRING'

    I hope it helps. BR,

    Alvaro

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Feb 23, 2005 at 08:20 AM

    Dear Friends

    Thanks for your xlent answers. Everything helped me.

    (I am currently working on 46C release ).

    I would like to use the REPLACE_STRING FM in my logic.

    But, I am not sure why the REPLACE_STRING FM does not work. I get an exception saying "STRING_NOT_FOUND".

    My inputs were as below.

    I_SEARCHSTRING = JUNE

    I_REPLACESTRING = JULY

    IO_STRING = I WAS BORN IN JUNE

    I see that REPLACE_STRING uses the same logic as the Do loop suggested by Serdar.

    Am I wrong somewhere ?

    Thanks

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Feb 23, 2005 at 09:54 AM

    Dear Lakshman

    Thanks. STRING_REPLACE is what I was looking for.

    Friends, I am not able to reward points to anyone.

    Because, for this particular post, I do not see symbols next to the posts for rewarding points.

    What do i do ?

    Thanks

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.