Search code examples
freemarker

Freemarker code to split consecutive numbers


I need to split consecutive numbers.

Case 1) 34123

Output: 34 123

Case 2) 123434123

Output: 1234 34 123


Solution

  • [#function splitConsecutiveNumbers input]
        [#assign output = ""]
        [#list 0..<input?length as i]
            [#if !i?is_last]
                [#if (input[i]?number+1) != input[i+1]?number]
                    [#assign output += input[i] + " "]
                [#else]
                    [#assign output += input[i]]
                [/#if]
            [#else]
                [#assign output += input[i]]
            [/#if]
        [/#list]
        [#return output]
    [/#function]
    
    ${splitConsecutiveNumbers("34123")}
    ${splitConsecutiveNumbers("123434123")}