在Arlesylist中将Arraylist转换为字符串

Nid*_*har 1 powershell

我试图从变量grep一些数据:

 Select-String -inputObject $patternstring  -Pattern $regex -AllMatches
 | % { $_.Matches } | % { $_.Value } -OutVariable outputValue
 Write-Host $outputValue
Run Code Online (Sandbox Code Playgroud)

对于同样的变量,我正在尝试进行字符串操作

$outputValue.Substring(1,$outputValue.Length-2);
Run Code Online (Sandbox Code Playgroud)

这失败说明outputValue是一个ArrayList.

我怎样才能转换ArraylistString

dea*_*dog 5

正如sean_m的注释中所提到的,最简单的方法是首先使用-join运算符将字符串的System.Collections.ArrayList转换为单个字符串:

$outputValue = $($outputValue -join [Environment]::NewLine)
Run Code Online (Sandbox Code Playgroud)

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

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