0

In Powershell how would I convert a string with a varying length of characters to an array formatted to a specific number of characters per item? Example using 10 characters per item in the array:

string: 012345678901234567890123456789

array:

0123456789
0123456789
0123456789

So the first item in the array would be the first 10 characters of the string, the second item the next 10, and so on. Thanks.

2 Answers 2

2
$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
Sign up to request clarification or add additional context in comments.

5 Comments

I like this method because it handles the remainder. It does need a -join "" method applied to the output though to recreate the string elements. Thanks.
Yeah, I forgot you were using strings so it would be better to use $a.substring(start,length).
i just get one item per line... not 10
@mpgn The even better part works. I've "improved" the first part. Simply added a -join to the line.
Thanks! I came from a google search but this was super helpful!
2

Here's one way to do that:

$a = '012345678901234567890123456789'

if($a.length % 10)
{
    for($i=0; $i -lt $a.length; $i+=10)
    {
        $a.Substring($i,10)    
    }
}
else
{
    "String length must devide in 10 without a remainder"
}

Another way:

if($a.length % 10)
{
    0..2 | Foreach {$a.Substring(($_*10),10)}
}
else
{
    "String length must devide in 10 without a remainder"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.