Reputation: 283
I am trying to replace a substring in a string based on certain condtions using COND #
. But not sure where to incorporate REPLACE
in the syntax.
Normal code:
DATA(v_string) = 'My string &1 &2'.
IF v_string CA '&1'.
REPLACE '&1' WITH 'sub1' in v_string.
ELSEIF v_string CA '&2'.
REPLACE '&2' WITH 'sub2' in v_string.
ENDIF.
I am trying to achieve the same using ABAP 7.5 syntax.
v_string = COND #( WHEN v_string CA '&1' THEN REPLACE '&1' WITH 'sub1' in v_string
WHEN v_string CA '&2' THEN REPLACE '&2' WITH 'sub2' in v_string ).
What is the correct syntax to get the desired results?
Upvotes: 2
Views: 9280
Reputation: 2988
As per documentation, the THEN operand
must be a a general expression position
, which is defined as
Reading position in which (alongside a suitable data object) a constructor expression, a table expression, a calculation expression, a built-in function, a functional method, or a method chaining can also be specified.
That means REPLACE
is not available, but you can use built-in string function replace.
v_string = COND #(
WHEN contains( val = v_string sub = '&1' )
THEN replace( val = v_string sub = '&1' with = 'sub1' occ = 1 )
WHEN contains( val = v_string sub = '&2' )
THEN replace( val = v_string sub = '&2' with = 'sub2' occ = 1 )
).
Upvotes: 6