2012-11-22 87 views
2

我需要一个更好的方法来做到这一点吗?多个拆分

$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] 

不喜欢在一个字符串上使用如此多的分割函数。我还能做什么?

我试图让在上面的例子中变量的个体的分辨率大小,颜色深度和频率到他们;

水平= 800 垂直= 600 颜色= 32 频率= 60

回答

6

我发现,我们可以通过字符数组到分割功能。
因此,在一行:

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

32 
bits 
@ 
60 
Hz. 
+0

你,先生,是个天才。 – Anicho

2

一个方法是:

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