powershell 从数组中删除最后一个逗号

wvyml7n5  于 2022-11-10  发布在  Shell
关注(0)|答案(1)|浏览(209)

创建了下面的代码,但是,我在要删除的数组中的最后一个字符串上得到了一个逗号。如何更改将逗号添加到数组的方式,或者简单地从数组中删除最后一个逗号?

clear
$dataOut = ''
$excludeList = 'choco*','kb*','dotnet*','netfx*','*.install*','*.portable*','vcredist*','packages*','lessmsi*'
$appsInstalled = Get-ChildItem -Exclude $excludeList -Recurse -Path "C:\ProgramData\chocolatey\lib\*.nuspec"

Foreach ($app in $appsInstalled) {
[xml]$XmlDocument = Get-Content $app.Fullname

# $appsInstalled = "$($XmlDocument.package.metadata.Title) v$($XmlDocument.package.metadata.Version)" + ", "

$appsInstalled = $XmlDocument.package.metadata.Title + " v" +$XmlDocument.package.metadata.Version + ", "
$dataOut = $dataOut + $appsInstalled
}
$dataOut
3bygqnnd

3bygqnnd1#

你所追求的被称为“修剪”。有三个箱子,从左、右和两端修剪。也就是说,从两个方向删除前导额外字符、尾随字符和。
$dataOut不是数组,而是字符串。可以使用.Net的字符串类的.Trim()方法。它将删除前导和尾随字符,如下所示

$s = "foo,bar,zof, "

# Print trimmed string, pipes will show that space was removed too

"|{0}|" -f $s.trim(@(',', ' '))
|foo,bar,zof|

相关问题