开发者

Powershell Using Backtick for new lines - weirdness

开发者 https://www.devze.com 2023-01-13 14:14 出处:网络
What exaxctly is happening in example 1? How is this parsed? # doesnt split on , [S开发者_JAVA技巧tring]::Join(\",\",(\"aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,\" + `

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

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号