string Powershell Array 以逗号分隔的字符串与引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39276437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Powershell Array to comma separated string with Quotes
提问by Eric
I have an array that I need to output to a comma separated string but I also need quotes "". Here is what I have.
我有一个需要输出到逗号分隔字符串的数组,但我还需要引号“”。这是我所拥有的。
$myArray = "file1.csv","file2.csv"
$a = ($myArray -join ",")
$a
The output for
$a
ends up
输出
$a
结束
file1.csv,file2.csv
My desired output is
我想要的输出是
"file1.csv","file2.csv"
How can I accomplish this?
我怎样才能做到这一点?
回答by Syphirint
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 '
.
你只需要找到一种方法来逃避"
. 所以,你可以通过放置它来做到这一点'
。
回答by abillon
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"