$num = '012345678901234567890123456789123' #Lenght is 33
#$num = '012345678901234567890123456789' #Lenght is 30
$split = 10
$len = $num.Length
$repeat=[Math]::Floor($len/$split)
for($i=0;$i-lt$repeat;$i++){
#$num[($i*$split)..($i*$split+$split-1)]
Write-Output (($num[($i*$split)..($i*$split+$split-1)]) -join '')
}
if($remainder=$len%$split){
#$num[($len-$remainder)..($len-1)]
Write-Output (($num[($len-$remainder)..($len-1)]) -join '')
}
Hope this helps
Even Better make it into a resuable function, like this:
function Split-ByLength{
<#
.SYNOPSIS
Splits string up by Split length.
.DESCRIPTION
Convert a string with a varying length of characters to an array formatted to a specific number of characters per item.
.EXAMPLE
Split-ByLength '012345678901234567890123456789123' -Split 10
0123456789
0123456789
0123456789
123
.LINK
http://stackoverflow.com/questions/17171531/powershell-string-to-array/17173367#17173367
#>
[cmdletbinding()]
param(
[Parameter(ValueFromPipeline=$true)]
[string[]]$InputObject,
[int]$Split=10
)
begin{}
process{
foreach($string in $InputObject){
$len = $string.Length
$repeat=[Math]::Floor($len/$Split)
for($i=0;$i-lt$repeat;$i++){
#Write-Output ($string[($i*$Split)..($i*$Split+$Split-1)])
Write-Output $string.Substring($i*$Split,$Split)
}
if($remainder=$len%$split){
#Write-Output ($string[($len-$remainder)..($len-1)])
Write-Output $string.Substring($len-$remainder)
}
}
}
end{}
}
$num1 = '012345678901234567890123456789' #Lenght is 30
$num2 = '012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789123123' #Lenght is 33
Split-ByLength -InputObject $num2 -Split 10