50

I would like to split a string witch has a variable whitespace characters, but a get a lot of empty lines which I would like to eliminate. this code

$text = "Video  Video  Audio  Audio  VBI    VBI"
$text.Split()

outputs this

Video

Video

Audio

Audio

VBI



VBI

PS H:\>

and I would like this

Video
Video
Audio  
Audio
VBI
VBI

Very late edit:
Noticed this question is still getting a lot of views, so I would like to clarify that I had no knowledge of Functional Programming or Regular Expressions when I asked this question.
All the solutions mentioned here apply as there are multiple ways to remove whitespace and create an array from a string.

1
  • Thank you for this question. I was also having an issue like this with the Split method. The number of spaces in my delimiter was fixed (each delimiter was 4 spaces as it was output from the reg QUERY command), but for whatever reason .Split(" ") would still give me the extra lines. Commented Sep 5 at 19:36

6 Answers 6

64

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

1 Comment

-split '\s+' doesn't remove leading and trailing whitespace, so you still need to filter out empty strings.
31

you can use this snippet to eliminate empty lines :

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

2 Comments

This doesn't work if there is a tab in between the strings.
@randomuser15995183 it does if you specify tab character "`t". Try this, press Ctrl-J to break a line. [char]9 is a Tab char: "1 2 $([char]9)3 4".split(" `n`t",[System.StringSplitOptions]::RemoveEmptyEntries)
28

-split "Video Video Audio Audio VBI VBI"

1 Comment

+1 I prefer this to my own answer. I forgot -split could be used as a unary operator.
8

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()

Comments

2

The -split operator takes a regex argument, so just match multiple whitespace characters (\s+):

$Text = $text = "Video  Video  Audio  Audio  VBI    VBI"
$text -split '\s+' -match '\S'

Video
Video
Audio
Audio
VBI
VBI

Any trailing whitespace after the last one may leave you will a null entry, so the -match will eliminate anything that is only whitespace.

1 Comment

k get replicasets | %{$_.split(" ") -match "\s"}
1

other solution :

$text -split ' ' | where {$_.Trim() -ne ''}

or :

$text.Split(' ').Where({$_.Trim() -ne ''})

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.