What exaxctly is happening in example 1? How is this parsed?
# doesnt split on ,
[S开发者_JAVA技巧tring]::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 { ('"' + $_ + '"') }))
In you first example you may remove the backtick, because Powershell knows that the string will continue (there is a +
sign).
What posh does
- takes string "aaaa,aaaa..." (1) from first
- evaluates the expression with split - it returns array of strings (from
"aaaa,...aaaa".Split(',')
) - converts the array of strings to string, which returns again string "aaaa,...aaaa"
- adds results from 1. and 3.
Note: when posh converts array to string, it uses $ofs
variable. You will see it better in action when you will try this code:
$ofs = "|"
[String]::Join(",", ("aaaaa,aaaaa" + "bbbb,bbbb,bbbb".Split(',') | foreach { ('"' + $_ + '"') }))
Your first example only has the Split method applied to the second string of a's. The parentheses are necessary for order of operations. The Split method is performed before the concatenation in your first example.
精彩评论