做textwrap和DEDENT语言在Windows PowerShell(或DOTNET又名.NET) [英] doing textwrap and dedent in Windows Powershell (or dotNet aka .net)

查看:269
本文介绍了做textwrap和DEDENT语言在Windows PowerShell(或DOTNET又名.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

Python有textwrap和DEDENT语言功能。他们这样做pretty的多少你希望给你提供任何字符串。

Python has "textwrap" and "dedent" functions. They do pretty much what you expect to any string you supply.

textwrap.wrap(文本[,宽度[,...]])

包装在文本(字符串)的单款所以每行最多为宽字符。返回的输出线名单,没有最后的换行符。

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(文字)

从文本每行中删除任何共同的前导空格。

Remove any common leading whitespace from every line in text.

http://docs.python.org/library/textwrap.html

问:

你如何做到这一点在Windows PowerShell中(或.NET方法从PowerShell中调用)?

How do you do this in Windows PowerShell, (or with .NET methods you call from PowerShell)?

推荐答案

这是一个疏忽code ...

This is a negligent code...

#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}"
}

这篇关于做textwrap和DEDENT语言在Windows PowerShell(或DOTNET又名.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆