09-29-2010 3:53 PM
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
09-29-2010 4:05 PM
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
09-29-2010 4:33 PM
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
09-29-2010 4:41 PM
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
09-29-2010 4:51 PM
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
09-29-2010 4:57 PM
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.
09-30-2010 9:23 AM
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