Board logo

标题: [转载代码] [PowerShell每日技巧]左对齐、右对齐(20140306) [打印本页]

作者: DAIC    时间: 2014-3-7 16:51     标题: [PowerShell每日技巧]左对齐、右对齐(20140306)

If you must make sure that a given string has a uniform width, then you can use .NET methods to pad the string appropriately:

PS C:\> $mytext = 'Test'
PS C:\> $paddedText = $mytext.PadLeft(15)
PS C:\> "Here is the text: '$paddedText'"
Here is the text: '           Test'
PS C:\>
PS C:\> $paddedText = $mytext.PadRight(15)
PS C:\> "Here is the text: '$paddedText'"
Here is the text: 'Test           '


You can even add a padding character yourself (if you do not want to pad with spaces):
PS C:\> 'Hello'.PadLeft(20, '.')
...............Hello
PS C:\> 'Hello'.PadRight(20, '_')
Hello_______________


http://powershell.com/cs/blogs/tips/archive/2014/03/06/padding-strings-left-and-right.aspx
作者: PowerShell    时间: 2014-3-8 00:32

PadLeft()
PadRight()
这两个方法,是string类的方法。string类,就是字符串类,是最基本的类。
http://msdn.microsoft.com/zh-cn/library/system.string.aspx




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2