【问题标题】:Adding substrings to array, and the deleting that substring from the parent string将子字符串添加到数组,并从父字符串中删除该子字符串
【发布时间】:2013-07-26 20:27:16
【问题描述】:

我不知道该怎么做。我目前的代码如下:

[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

我也不知道为什么。

【问题讨论】:

    标签: arrays string powershell substring


    【解决方案1】:

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

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

    【讨论】:

    • 不知道你可以这样做。
    【解决方案2】:

    你把括号弄乱了

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

    【讨论】:

    • 该死的,谢谢!我几乎一直在学习powershell。需要深入了解语法。
    猜你喜欢
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多