# Split String into incremental numbers

Hi all,

I have a spread sheet of codes that have a corresponding string that contains the position of the bar in the code.
For example: Code 638 = 256811
The bar positions can be from 1 to 11, so in the example above I would look up 638 and the result would be 2, 5, 6, 8 and 11.
As 10 and 11 are not delimited, how would I turn the string into a list of incremental numbers, including possible 2 digit numbers.
I’d be grateful for any ideas
Cheers
Ian

Hi.

If we assume the numbers are in sequence, the first digit of a two-digit number will be less than or equal to the previous digit (or previous first-digit):

``````on splitNumberStr(numberStr)
set numberStr to numberStr as text -- Just in case.

set splitList to {}
set previousDigit to "0"
set i to 1
set strCount to (count numberStr)
repeat until (i > strCount)
set thisDigit to character i of numberStr
if (thisDigit > previousDigit) then
set end of splitList to thisDigit
set i to i + 1
else
set end of splitList to text i thru (i + 1) of numberStr
set i to i + 2
end if
set previousDigit to thisDigit
end repeat

return splitList
end splitNumberStr

splitNumberStr("256811")
``````

Edit: The above solution is strictly good enough for the problem posed, but is only good for numbers up to “19”. This variation works up to “99”, again assuming that the numbers in the string to be split are in order:

``````on splitNumberStr(numberStr)
set numberStr to numberStr as text -- Just in case.

set splitList to {}
set previousResultAsNumber to 0
set i to 1
set strCount to (count numberStr)
repeat until (i > strCount)
set thisDigit to character i of numberStr
if (previousResultAsNumber < thisDigit) then -- Numerical rather than string comparison.
set end of splitList to thisDigit
set i to i + 1
else
set end of splitList to text i thru (i + 1) of numberStr
set i to i + 2
end if
set previousResultAsNumber to (end of splitList) as integer -- The whole of the previous result in number form.
end repeat

return splitList
end splitNumberStr

splitNumberStr("12345678910111213141516171819202122232432475099")
``````

That’s great, Nigel.

Whilst I am reliably informed the maximum is 11, it is prudent to be able to go up to 99.

Many thanks