【发布时间】:2021-02-26 03:31:20
【问题描述】:
我希望在 PowerShell 中运行长字符串句子,并在每 32 个字符 + 空格后返回一个新行(以避免在单词中间添加新行)。到目前为止,这是我尝试过的:
$Runonsentence = 'Interestingly, Bryan A. Garners "The Oxford Dictionary of American Usage and Style" states that while there is a distinction between run-on sentences and comma splices, it isnt typically noteworthy. However, Garner also adds "The distinction can be helpful in differentiating between the wholly unacceptable (true run-on sentences) and the usually-but-not-always unacceptable (comma splices).'
Filter Get-Stringy {
for($num = 0; $num -le $_.Length-32; $num+=32) {
$indexer = $_.Substring($num, 32)
$parser = $indexer.LastIndexOf(" ", 32)
$trimmer = $indexer.Split($parser)
$trimmer.Trim()
}
}
$Runonsentence | Get-Stringy
这是它返回的内容:
Interestingly, Bryan A. Garners
"The Oxford Dictionary of Americ
an Usage and Style" states that
while there is a distinction bet
ween run-on sentences and comma
splices, it isnt typically notew
orthy. However, Garner also adds
"The distinction can be helpful
in differentiating between the
wholly unacceptable (true run-on
sentences) and the usually-but-
not-always unacceptable (comma s
您可能从上面的结果中可以看到,末尾的单词被截断,整个字符串没有被遍历或显示。如果有不同的策略或方法可以做到这一点,我们将不胜感激!
【问题讨论】:
-
嗨,有什么对你有用的吗?如果您需要更多帮助,请通过评论告知。
-
是的,它工作得很好,谢谢。
-
我的声誉刚刚超过了我可以这样做的门槛,所以谢谢!
-
成功了。不过,请参阅下面的最新帖子。由于其他原因,我正在尝试撤消替换。
标签: regex string powershell split