示例1中发生了什么?这是如何解析的?
# doesnt split on ,
[String]::Join(",",("aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa," + `
"aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa".Split(',') `
| foreach { ('"' + $_ + '"') }))
# adding ( ) does work
[String]::Join(",",(("aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa," + `
"aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa").Split(',') `
| foreach { ('"' + $_ + '"') }))
答案 0 :(得分:2)
在第一个示例中,您可以删除反引号,因为Powershell知道该字符串将继续(有一个+
符号)。
豪华做什么
"aaaa,...aaaa".Split(',')
)注意:当posh将数组转换为字符串时,它使用$ofs
变量。当您尝试使用此代码时,您会看到更好的操作:
$ofs = "|"
[String]::Join(",", ("aaaaa,aaaaa" + "bbbb,bbbb,bbbb".Split(',') | foreach { ('"' + $_ + '"') }))
答案 1 :(得分:1)
您的第一个示例仅将Split方法应用于a的第二个字符串。括号是操作顺序所必需的。 Split方法在第一个示例中的连接之前执行。