将子字符串添加到数组,并从父字符串中删除子字符串

时间:2013-07-26 20:27:17

标签: arrays string powershell substring

我无法弄清楚如何做到这一点。我目前的代码如下:

[string]$strTest = Read-Host "Please input an IP host address in full dotted decimal.`n(Example: 192.168.004.214)"
[array]$arrTest = 0
$str = $strTest
$fs = "."
$index = $str.indexof($fs)
$count = 1
do{
    $chunk = $str.substring(0,$index)
    $arrTest += $chunk
    $count++
    $str = $str -replace ($chunk + ".", "")
    }
    until($count -eq 4) 

$arrTest    

我希望这能给我一个填充了IP地址的每个八位字节的数组,但是我得到了一个奇怪的结果。在ISE中运行它给了我:

0
123
123
123

我不明白为什么。

2 个答案:

答案 0 :(得分:1)

如果你想要一个包含八位字节的数组,只需拆分字符串:

$a = $str.Split('.')

答案 1 :(得分:0)

你刚搞砸了括号

$str = $str -replace ($chunk + "."), ""