背景
Python 有“textwrap”和“dedent”函数。它们对您提供的任何字符串的作用几乎符合您的预期。
textwrap.wrap(text[, width[, ...]])
Wraps the single paragraph in text (a string) so every line is at most width characters long. Returns a list of output lines, without final newlines.
textwrap.dedent(text)
Remove any common leading whitespace from every line in text.
http://docs.python.org/library/textwrap.html
问题:
如何在 Windows PowerShell 中执行此操作(或使用从 PowerShell 调用的 .NET 方法)?
最佳答案
这是一个疏忽的代码...
#requires -version 2.0
function wrap( [string]$text, [int]$width ) {
$i=0;
$text.ToCharArray() | group { [Math]::Floor($i/$width); (gv i).Value++ } | % { -join $_.Group }
}
function dedent( [string[]]$text ) {
$i = $text | % { $_ -match "^(\s*)" | Out-Null ; $Matches[1].Length } | sort | select -First 1
$text -replace "^\s{$i}"
}
关于.net - 在 Windows Powershell(或 dotNet 又名 .net)中执行 textwrap 和 dedent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1417663/