Splitting a String into an Array of Strings with equal length

Let’s say you have to split a string equally every 6 characters.

PowerShell’s split function and some regular expressions will do just that:

1
2
$str = "NB7962NB1248NB6587NB9426"
$arr = $str -split "(\w{6})"

This will return an array with the following elements:

NB7962
NB1248
NB6587
NB9426

Leave a Reply

Your email address will not be published. Required fields are marked *