gpt4 book ai didi

powershell - 在 powershell 中将 Arraylist 转换为字符串

转载 作者:行者123 更新时间:2023-12-02 22:54:09 24 4
gpt4 key购买 nike

我正在尝试从变量中获取一些数据:

 Select-String -inputObject $patternstring  -Pattern $regex -AllMatches
| % { $_.Matches } | % { $_.Value } -OutVariable outputValue
Write-Host $outputValue

对于同一个外变量,我正在尝试进行字符串操作

$outputValue.Substring(1,$outputValue.Length-2);

这说明 outputValue 是一个 ArrayList 失败。

如何将 Arraylist 转换为 String

最佳答案

如 sean_m 的评论中所述,最简单的做法是首先使用 -join 运算符将字符串的 System.Collections.ArrayList 转换为单个字符串:

$outputValue = $($outputValue -join [Environment]::NewLine)

完成此操作后,您可以对 $outputValue 执行任何常规字符串操作,例如 Substring() 方法。

上面我用新行分隔 ArrayList 中的每个字符串,因为这通常是 -OutVariable 在将其转换为 ArrayList 时拆分字符串的字符串,但如果需要,您可以使用不同的分隔符/字符串。

关于powershell - 在 powershell 中将 Arraylist 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12909960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com