Anicho
Anicho

Reputation: 2667

multiple splittings

I need a better way of doing this any ideas?

$strOutput = "800x600, 32 bits @ 60 Hz."

      # Initial split
$aSplitString = $strOutput.Split(",")


# Get Horizontal and Vertical Length
$aSplitString2 = $aSplitString[0].Split("x")
$strHorizontal = $aSplitString2[0]
$strVertical = $aSplitString2[1]
$aSplitString2 = $null

#Get Color Depth and Frequency
$aSplitString2 = $aSplitString[1].Split(" ")
$strColour = $aSplitString2[1]
$strFrequency = $aSplitString2[4]

Not a fan of using so many split functions on one string. What else could I do?

I am trying to get the individual resolution sizes, the color depth and the frequency into their on variables in the above example;

horizontal = 800 vertical = 600 color = 32 frequency = 60

Upvotes: 2

Views: 5559

Answers (2)

Loïc MICHEL
Loïc MICHEL

Reputation: 26180

I've found that we can pass an array of chars to the split function.
So, in one line :

PS C:\Windows\system32> "800x600, 32 bits @ 60 Hz.".split(@("x",","," "))
800
600

32
bits
@
60
Hz.

Upvotes: 6

CB.
CB.

Reputation: 60976

one way is:

$strOutput = "800x600, 32 bits @ 60 Hz."
$splitted = $strOutput -replace '\D',' ' -split '\s+'
$strHorizontal = $splitted[0] 
$strVertical = $Splitted[1]
$strColour = $splitted[2]
$strFrequency = $splitted[3]

Upvotes: 2

Related Questions