首页 > 解决方案 > 拆分产生行数组而不是段落

问题描述

我正在尝试将字符串分成段落,分隔线位于字符“/*”的出现之间。当前使用 split 函数创建一个数组,其中每个元素都是文本的新行而不是整个段落。在下面我当前使用的代码中,我已经转义了 * 但似乎我仍然遗漏了一些东西。谢谢。

$scripts = $allScripts -split "/\*"

预期输出:

$scripts[0] = "ABC
DEF
GHI"

实际输出:

$scripts[0] = "ABC"
$scripts[1] = "DEF"
$scripts[2] = "GHI"

标签: arrayspowershellsplit

解决方案


You can use join from the string library, to join by the newline character:

> [String]::Join("`n", $allScripts -split "/\*")
ABC
DEF
GHI

This is general purpose, for concatenating string[] to a single multi-line string.

However, in this specific case @AdminOfThings has the more elegant solution, replacing your special new line characters, with the actual new line character:

> $allScripts -replace "/\*", "`n"
ABC
DEF
GHI

推荐阅读