vamsi
4 years agoNew Contributor II
Split String by appending a new value after every 3 characters from the end
Hi All,
Need help with the below scenario…
I have a value A341B22C01D91E80 I wanted to have this split from the end after every 3 characters and append with +
So my output should be A+341+B22+C01+D91+E80
- should be added after every 3 chars from the end and based on the length of the value passed (Length will be dynamic)
Thanks in advance.
Using a mapper and assuming your input text is at the key
$text
you can use the following expression to achieve your goal:$text.split("").reverse().reduce((accum, cur) => (accum.length - accum.split("+").length +1) % 3 == 0 ? accum + "+" + cur: accum + cur).split("").reverse().join("")