我喜欢 @Leo 和 @rojo 的答案背后的意图,但拆分行不适合大量文本,并且状态机(或者更好的是,一个易于编程的......像正则表达式)会更有效率。
除了编写我自己的复杂解决方案之外,我还保证了它的复杂性,因为它在原始字符串中保留换行符,甚至允许您对某些字符强制换行
Wrap -Length 30 -Force '.' "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
在这里,我尝试打断大约 30 个字符,找到最近的空格,但也强制在句号处打断,忽略单词中的句点,并确保不要打断因句号而已经中断的行。
Lorem Ipsum is simply dummy
text of the printing and
typesetting industry.
Lorem Ipsum has been the
industry's standard dummy text
ever since the 1500s, when an
unknown printer took a galley
of type and scrambled it to
make a type specimen book.
It has survived not only five
centuries, but also the leap
into electronic typesetting,
remaining essentially
unchanged.
It was popularised in the
1960s with the release of
Letraset sheets containing
Lorem Ipsum passages, and more
recently with desktop
publishing software like Aldus
PageMaker including versions
of Lorem Ipsum.
这是函数本身,但请注意,您需要此答案底部的内容来生成正则表达式(我把它放在更高的位置,以便您可以阅读参数)
Function Wrap {
Param (
[int]$Length=80,
[int]$Step=5,
[char[]]$Force,
[parameter(Position=0)][string]$Text
)
$key="$Length $Step $Force"
$wrap=$_WRAP[$key]
if (!$wrap) {
$wrap=$_WRAP[$key]=_Wrap `
-Length $Length `
-Step $Step `
-Force ($Force -join '') `
| Concat -Join '|' -Wrap '(',')(?:[^\n\r\S])+'
}
return $Text -replace $wrap,$_WRAP['']
}
这里有一些更有意义的东西来展示它的多功能性。
不要担心脚本的其余部分,它是 colours/backgrounds、emailing 或 GridView。
它基本上是在做一堆 ping、tcp 检查和 http 请求,并将错误消息放入 info 属性中:
$_.info=(
$style.bf.yellow, `
(Wrap -Length 55 -Force ':.' $_.info), `
$colour `
| Concat)
您将需要上面定义的以下这些实用函数Wrap
Function Concat {
Param ([switch]$Newlines, $Wrap, $Begin='', $End='', $Join='')
Begin {
if ($Newlines) {
$Join=[System.Environment]::NewLine
}
$output=[System.Text.StringBuilder]::new()
$deliniate=$False
if (!$Wrap) {
$output.Append($Begin) | Out-Null
}
elseif ($Wrap -is [string]) {
$output.Append(($End=$Wrap)) | Out-Null
}
else {
$output.Append($Wrap[0]) | Out-Null
$End=$Wrap[1]
}
}
Process {
if (!($_=[string]$_).length) {
}
elseif ($deliniate) {
$output.Append($deliniate) | Out-Null
$output.Append($_) | Out-Null
}
else {
$deliniate=$Join
$output.Append($_) | Out-Null
}
}
End {
$output.Append($End).ToString()
}
}
$_WRAP=@{''="`$1$([System.Environment]::NewLine)"}
Function _Wrap {
Param ($Length, $Step, $Force)
$wrap=$Force -join '' -replace '\\|]|-','\$0'
$chars="^\n\r$wrap"
$preExtra="[$chars\S]*"
$postExtra="[^\s$wrap]"
$chars="[$chars]"
$postChars="$preExtra$postExtra"
if ($wrap) {
$wrap="[$wrap]"
$wrap
$wrap="$wrap(?=\S)"
$chars="$chars|$wrap"
$postChars="$postChars|$preExtra$wrap"
}
for (
($extra=0),($next=$NULL),($prev=$NULL);
($next=$Length - $Step) -gt 0 -and ($prev=$extra + $Step);
($Length=$next),($extra=$prev)
) {
"(?:$chars){$next,$Length}(?=(?:$postChars){$extra,$prev})"
}
}