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: 

Replace until to one character

TimoRenz
Participant
0 Kudos

Hi,

i have the following string for example: 'B B B B'. Is it possible to replace three of them with SPACE and get only one 'B'?

The problem is that it is also possible to get only 'B B' or 'B'. There i should also get only one 'B'.

I tried this with the addition REPLACEMENT OFFSET but it doesn`t work because i do a CONDENSE after replacing and so the offset is always 0.

Any ideas?

Best regards,

Timo

6 REPLIES 6

ThomasZloch
Active Contributor
0 Kudos

Based on your description, I propose the following solution:

lf_string = 'B'.

This is probably not what you're looking for, so please provide a more descriptive example, maybe with actual values.

Thomas

SimoneMilesi
Active Contributor
0 Kudos

I'm sure there is a better way, but this works for sure.


DATA: new_string LIKE old_string.
DO.
MOVE old_string TO new_string.
REPLACE wanted_char INTO new_string WITH space.

if NOT new_string CA wanted_char.
MOVE old_string to new_string.
EXIT.
ENDIF.
ENDDO.
WRITE: /1 new_string.

Where OLD_STRING = your initial string,

WANTED_CHAR = the char you want to search and replace until only 1 remain

NEW_STRING = The final string

Former Member
0 Kudos

Hi,

try this way...

call first occurance three times to replace with new character with out Offset...as per requirement

REPLACE first OCCURRENCES OF 'B' IN w_string WITH space.

REPLACE first OCCURRENCES OF 'B' IN w_string WITH space.

REPLACE first OCCURRENCES OF 'B' IN w_string WITH space'.

Prabhudas

Former Member
0 Kudos

Hi,

try this...


DATA: l_text TYPE string,
      l_rule TYPE char2.

l_text = 'B B'.

IF STRLEN( l_text ) > 1.
  l_rule = l_text(1).
  CONCATENATE l_rule ' ' INTO l_rule.
  TRANSLATE l_text USING l_rule.
  CONCATENATE l_rule(1) l_text+1 INTO l_text.
ENDIF.
write l_text.

Regards

Matus

kesavadas_thekkillath
Active Contributor
0 Kudos

So you need to replace the consecutive characters. You can check the regex statements in which this can be done very easily.But im not sure how to build the pattern for this case that too retaining 1 occurrence of consecutive characters.

As of now you can check this code


DATA:lv_consec TYPE char255 VALUE 'B B B B B B B A A X X'.
DATA:len TYPE i,
     l_off TYPE i.
FIELD-SYMBOLS:<fs>,<fs1>.
condense lv_consec no-gaps.
len = STRLEN( lv_consec ).
l_off = 0.


DO len TIMES.
  IF sy-index = 1.
    ASSIGN lv_consec+l_off(1) TO <fs>.
  ELSE.
    ASSIGN lv_consec+l_off(1) TO <fs1>.
    IF <fs> EQ <fs1>.
      <fs> = ' '.
    ENDIF.
    ASSIGN lv_consec+l_off(1) TO <fs>.
  ENDIF.
  l_off = l_off + 1.
ENDDO.
CONDENSE lv_consec no-gaps.
WRITE lv_consec.

Former Member
0 Kudos

Use REPLACE ALL OCCURRENCES

Also check then following link

http://wiki.sdn.sap.com/wiki/display/HOME/MessageMappingReplaceString

Edited by: kk.adhvaryu on Sep 30, 2010 10:25 AM