I am new to mulesoft
I have a flow variable named logPrefix
with value "Id1 - 123 - Id2 - 456"
I want to use the set-variable component and change the value of this variable to "Id1 - 123"
.
Note that I don't want to do this vars.logPrefix[0 to 8]
I would like to do something cleaner based on finding the index of "Id2" within the string logPrefix
and using substring on logPrefix
using this index
If set-variable isn't the best way to do this, let me know that too please
TIA
You can try below approach .
Edit: Based on @aled suggestion I have updated substringBefore instead of splitBy which will give all elements before "Id2". Since your desired output is before last "-" from above result, I have used substringBeforeLast which will get all the elements before "-" . Further trim will remove extra spaces after "-"
%dw 2.0
output application/json
import * from dw::core::Strings
var logPrefix="Id1 - 123 - Id2 - 456"
---
trim(substringBeforeLast((substringBefore(logPrefix,"Id2")),"-"))
Output
"Id1 - 123"