Created
April 4, 2023 13:06
-
-
Save drlsdee/8e4ac1ac3b572da76e13b97023cbe31e to your computer and use it in GitHub Desktop.
Just an example of a function that splits the input string into a set of substrings of a given length.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Split-Array { | |
[CmdletBinding()] | |
param ( | |
[Parameter()] | |
[char[]] | |
$InputObject, | |
[Parameter()] | |
[int] | |
$PartSize | |
) | |
begin {} | |
process { | |
for ($index = 0; $index -lt $InputObject.Count; $index += $PartSize) { | |
[int]$arrayTail = $InputObject.Count - $index | |
[int]$buffSize = [System.Math]::Min($arrayTail,$PartSize) | |
[char[]]$buffer = [char[]]::new($buffSize) | |
[array]::Copy($InputObject,$index,$buffer,0,$buffSize) | |
[string]::new($buffer) | |
} | |
} | |
end {} | |
} | |
Split-Array -InputObject ([guid]::NewGuid().ToString('N').ToCharArray()) -PartSize 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment