Split string with variable whitespace characters in Powershell

you can use this snippet to eliminate empty lines :

$text.split(" ",[System.StringSplitOptions]::RemoveEmptyEntries)

You can use PowerShell's -split operator which uses regular expressions.

"Video  Video  Audio  Audio  VBI    VBI" -split '\s+'

As noted by @StijnDeVos, this does not remove leading/trailing whitespace.

Here, the \s represents whitespace characters, and the + matches one or more of them. All the more reason to go with @user3554001's answer.

Another option is to filter the empty strings.

 "Video  Video  Audio  Audio  VBI    VBI".split()| where {$_}

-split "Video Video Audio Audio VBI VBI"


Try this, it replaces more than one instance of a space with a single instance before carrying out the split command:

$($text -replace '\s+', ' ').split()

Tags:

Powershell