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: 

Search and replace last 4 caracteres in a string

Former Member
0 Kudos

Hi Guys,

First and foremost moderators, I do know abap, have been working for many years with it.

Had an issue where I lost partially my memory and only now after a couple of years I am back in the workforce.

So please do not delete my post. Would be very helpful to get some answers.

So, in one of the three interfaces I am working on I need to:

Search for 'av.' in a string.

If 'av.' is found, it needs to be replaced by 'avenue'

If nothing is found string stays as it is.

I have a long code and for some reason my mind only remembers the difficult stuff ...

On my code:

Data: V_string TYPE String.

And after on the code:

V_string = ls_itab-street.

Thank you all!

F

5 REPLIES 5

Former Member
0 Kudos

DATA: T(10) VALUE 'av.defghij',
      STRING LIKE T,
      STR1(3) VALUE 'av.',
      STR2(6) VALUE 'avenue',

STRING = T.


IF STRING CS STR1

REPLACE STR1 WITH STR2 INTO STRING.

ENDIF.

0 Kudos

Hi Santhoshini,

There may be some issues here. The requirement here is to search and replace in the last 4 characters. Also, the direct 'REPLACE' may eliminate the 'av.' from other portions of the text also if present which is not correct.

Thanks,

Anupam

anupam_anand
Participant
0 Kudos

Hi Fafa,

Please find the code below:

DATA: lv_str1 TYPE c LENGTH 40,

      lv_str2 TYPE c LENGTH 40,

      lv_line TYPE i.

lv_str1 = 'Anupam Anand av.'.

lv_line = strlen( lv_str1 ).

lv_line = lv_line - 4.

lv_str2 = lv_str1+lv_line(4).

IF lv_str2 ca 'av.'.

REPLACE ALL OCCURRENCES OF 'av.' in lv_str2 WITH 'avenue'.

lv_str1+lv_line(4) = ''.

CONCATENATE lv_str1 lv_str2 INTO lv_str1.

ENDIF.

WRITE: lv_str1.

Hope this helps

Thanks,

Anupam

former_member209685
Participant
0 Kudos

Hi Fafa,

there you go. The easiest solution imho is using regular expressions. A very useful thing to learn.

REPORT  zav_replace_test.

DATA: lt_testdata TYPE TABLE OF string,

      lv_string LIKE LINE OF lt_testdata.

lv_string = 'Text with av. in the middle'.

INSERT lv_string INTO TABLE lt_testdata.

lv_string = 'Navigation abbreviated as Nav.'.

INSERT lv_string INTO TABLE lt_testdata.

lv_string = 'Navigation abbreviated as Nav. in a sentance'.

INSERT lv_string INTO TABLE lt_testdata.

lv_string = 'Palm av.'.

INSERT lv_string INTO TABLE lt_testdata.

LOOP AT lt_testdata INTO lv_string.

  WRITE: / 'Before replace:', lv_string.

  " Explanation of the regex

  " \s one white space

  " av the text av

  " \. a literale .

  " $ the end of the string

  REPLACE REGEX '\sav\.$' IN lv_string WITH ' Avenue'.

  WRITE: / 'After replace:', lv_string.

ENDLOOP.

Former Member
0 Kudos

Find your words whatever you want to search by using 'CS'. Take that word into a variable and replace it by using replace command.

Regards

Vivek