Forum Discussion

vamsi's avatar
vamsi
New Contributor II
4 years ago
Solved

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

3 Replies

  • 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's avatar
      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.

  • 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('+')