且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在PowerShell中的新行上拆分带有空格的字符串

更新时间:2021-12-24 07:36:20

String.Split() 是要拆分的字符列表,而不是要匹配然后拆分的字符序列.您现有的代码将在换行符处拆分,并将在空格处拆分.

The string argument in String.Split() is a list of characters to split on, not a sequence of characters to match and then split on. Your existing code will split on newline, and will split on space.

如果您只想在换行符上拆分,请使用:

If you only want to split on newline, use:

.split("`n")

如果要在换行符后跟空格的字符序列上进行拆分,可以使用 Regex.Split():

If you want to split on the character sequence of a newline followed immediately by a space, you can use Regex.Split():

[Regex]::Split($entry.'Group Name',"`n ") | ...

或者,您可以使用 -split 运算符,它也可以按字符串而不是字符列表进行拆分:

Alternately, you can use the -split operator, which also splits by a string and not a list of characters:

$entry.'Group Name' -split "`n "