[英]Powershell Array to comma separated string with Quotes
I have an array that I need to output to a comma separated string but I also need quotes "".我有一个数组,我需要将 output 转换为逗号分隔的字符串,但我还需要引号“”。 Here is what I have.
这就是我所拥有的。
$myArray = "file1.csv","file2.csv"
$a = ($myArray -join ",")
$a
The output for $a
ends up $a
的 output 结束了
file1.csv,file2.csv
My desired output is我想要的 output 是
"file1.csv","file2.csv"
How can I accomplish this?我怎样才能做到这一点?
Here you go: 干得好:
[array]$myArray = '"file1.csv"','"file2.csv"'
[string]$a = $null
$a = $myArray -join ","
$a
Output: 输出:
"file1.csv","file2.csv"
You just have to get a way to escape the "
. So, you can do it by putting around it '
. 你只需要获得一种方式来逃避
"
。所以,你可以通过把它周围做到这一点'
。
I know this thread is old but here are other solutions 我知道这个线程已经老了,但这里有其他解决方案
$myArray = "file1.csv","file2.csv"
# Solution with single quote
$a = "'$($myArray -join "','")'"
$a
# Result = 'file1.csv','file2.csv'
# Solution with double quotes
$b = '"{0}"' -f ($myArray -join '","')
$b
# Result = "file1.csv","file2.csv"
If using PowerShell Core (currently 7.1), you can use Join-String如果使用 PowerShell Core(当前为 7.1),您可以使用Join-String
This is not available in PowerShell 5.1这在 PowerShell 5.1 中不可用
$myArray | Join-String -DoubleQuote -Separator ','
Output:输出:
"file1.csv","file2.csv"
Here's a Join-String method that will work in older methods of PowerShell这是一个适用于旧的 PowerShell 方法的 Join-String 方法
function Join-String {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)] [string[]]$StringArray,
$Separator=",",
[switch]$DoubleQuote=$false
)
BEGIN{
$joinArray = [System.Collections.ArrayList]@()
}
PROCESS {
foreach ($astring in $StringArray) {
$joinArray.Add($astring) | Out-Null
}
}
END {
$Object = [PSCustomObject]@{}
$count = 0;
foreach ($aString in $joinArray) {
$name = "ieo_$($count)"
$Object | Add-Member -MemberType NoteProperty -Name $name -Value $aString;
$count = $count + 1;
}
$ObjectCsv = $Object | ConvertTo-Csv -NoTypeInformation -Delimiter $separator
$result = $ObjectCsv[1]
if (-not $DoubleQuote) {
$result = $result.Replace('","',",").TrimStart('"').TrimEnd('"')
}
return $result
}
}
It can be invoked with array parameter or passthrough可以通过数组参数或直通调用
Join-String @("file1.txt","file2.txt","file3.txt") -DoubleQuote
Output:输出:
"file1.txt","file2.txt","file3.txt"
or as pass-thru:或作为直通:
@("file1.txt","file2.txt","file3.txt") | Join-String -DoubleQuote
Output:输出:
"file1.txt","file2.txt","file3.txt"
Without -DoubleQuote没有 -DoubleQuote
@("file1.txt","file2.txt","file3.txt") | Join-String
Output:输出:
file1.txt,file2.txt,file3.txt
or with a custom separator, say a semicolon或使用自定义分隔符,例如分号
@("file1.txt","file2.txt","file3.txt") | Join-String -DoubleQuote -Separator ";"
Output:输出:
"file1.txt";"file2.txt";"file3.txt"
Supposing there is an array or list inside $myArray
.假设
$myArray
中有一个数组或列表。 Defined such as:定义如:
$myArray = @("one", "two")
We have two solutions:我们有两种解决方案:
Using double-quotes ( " ) as separator:使用双引号 ( " )作为分隔符:
'"{0}"' -f ($myArray -join '","')
Outputs:输出:
"one","two"
Using single-quotes ( ' ) as separator:使用单引号 ( ' )作为分隔符:
"'{0}'" -f ($myArray -join "','")
Outputs:输出:
'one','two'
Just a slight addition to @Jason S's answer, adding a carriage return (and line feed - a Windows thing):只是对@Jason S 的回答稍作补充,添加一个回车符(和换行符 - 一个 Windows 的东西):
$myArray | Join-String -DoubleQuote -Separator `r`n
And with the comma:用逗号:
$myArray | Join-String -DoubleQuote -Separator ",`r`n"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.