Powershell split() vs -split - what's the difference?

后端 未结 3 735
萌比男神i
萌比男神i 2020-12-07 05:18

After struggling with this for half an hour, I\'ve experienced this difference when splitting a string with spaces, depending on which syntax you use.

Simple string:

3条回答
  •  执念已碎
    2020-12-07 05:39

    The .Net System.String.Split method does not have an overload that takes a single parameter that is a string. It also does not understand regex.

    What is happening is that powershell is taking the string you are passing in and converting it to an array of characters. It is essentially splitting at the following characters :, \, s, +

    When you use ": " as the delimiter, I would imagine you got results like

    1
    
    2
    
    3
    
    4
    
    5
    

    That is because without specifying a string split option to the .Net method, it will include empty strings that it finds between adjacent separators.

提交回复
热议问题