arrays - 拆分产生行数组而不是段落
问题描述
我正在尝试将字符串分成段落,分隔线位于字符“/*”的出现之间。当前使用 split 函数创建一个数组,其中每个元素都是文本的新行而不是整个段落。在下面我当前使用的代码中,我已经转义了 * 但似乎我仍然遗漏了一些东西。谢谢。
$scripts = $allScripts -split "/\*"
预期输出:
$scripts[0] = "ABC
DEF
GHI"
实际输出:
$scripts[0] = "ABC"
$scripts[1] = "DEF"
$scripts[2] = "GHI"
解决方案
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
推荐阅读
- mysql - 可重复读取隔离级别 SELECT vs UPDATE...WHERE
- python - 迭代匹配子字符串,然后删除匹配项
- bash - 当一个程序在链中失败时如何停止重定向标准输出?
- spring - 创建名为“vendorController”的 bean 时出错:通过字段“vensorinterface”表达的依赖关系不满足;
- regex - 用于路径验证的正则表达式
- python - AttributeError:“系列”对象没有属性“isoweekday”
- pixi.js - PIXI V5 PIXI.Loader() 无法添加到图像容器
- python - Python selenium - 与主机、端口、用户名、密码的代理连接
- dynamic - Flurl:GetJsonListAsync 返回动态对象列表
- wordpress - 在 wordpress 中包含一个类