.net - 在 Windows Powershell(或 dotNet 又名 .net)中执行 textwrap 和 dedent

标签 .net python string text powershell

背景

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/

相关文章:

c++ - 如何修改函数中的字符串?

c# - MVC View 数据传递

python - Django 和 "Ordinary"Python 类成员和实例成员的区别?

python - 如何在 python 中对数组进行重采样

python - 更改字符串输入的格式

java - 为什么在填充和组装二维数组矩阵时会出现错误?

.net - XamlReader.Load找不到静态资源

c# - 如何从基类列表中获取子类对象?

c# - 无法解析/使用 System.ServiceModel.Security.WSTrustServiceContract 作为服务名称

java - java中的String有长度限制吗?