2

What exaxctly is happening in example 1? How is this parsed?

    # 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 { ('"' + $_ + '"') }))  

2 Answers 2

2

In you first example you may remove the backtick, because Powershell knows that the string will continue (there is a + sign).

What posh does

  1. takes string "aaaa,aaaa..." (1) from first
  2. evaluates the expression with split - it returns array of strings (from "aaaa,...aaaa".Split(','))
  3. converts the array of strings to string, which returns again string "aaaa,...aaaa"
  4. 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 { ('"' + $_ + '"') }))  
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.