Skip to content

Instantly share code, notes, and snippets.

@drlsdee
Created April 4, 2023 13:06
Show Gist options
  • Save drlsdee/8e4ac1ac3b572da76e13b97023cbe31e to your computer and use it in GitHub Desktop.
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.
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