cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Split String by appending a new value after every 3 characters from the end

vamsi
New Contributor II

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.

1 ACCEPTED SOLUTION

cjhoward18
Employee
Employee

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("")

View solution in original post

3 REPLIES 3

cjhoward18
Employee
Employee

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("")

vamsi
New Contributor II

Thank you @bojanvelevski It worked like a charmโ€ฆ

The second one needs some adjustments in terms of what was required but both can be used for the requirement.

You are a RockStar.

bojanvelevski
Valued Contributor

Hereโ€™s another one:

sl.range(0,$str.length+1).filter(x=>x%3==0).reverse().map(z=>$str.substring(z-2,z+1)).reverse().join('+')