|
- PowerShell 使用特殊文本命令 -- 字符串操作
格式化操作符 –F 在PowerShell文本操作符中非常重要,经常被用来增强数字类型和日期类型的可读性:
"{0} diskettes per CD" -f (720mb/1.44mb)
500 diskettes per CD
所有的基本操作符形式都大同小异,要处理的数据位于操作符的左右两边,然后通过操作符建立连接。例如,你可以使用下面的语句将文本中指定的字符串替换成目标文本:
“Hello Carl” -replace “Carl”, “Eddie”
Hello Eddie
-replace操作符有三种实现方式,其它文本操作符也类似地有三种实现方式,像-replace,-ireplace,-creplace,i前缀表示字符串大小写不敏感(insensitive),c前缀表示字符串大小写敏感(case sensitive)。
#下面的例子没有完成替换,因为当前大小写敏感:
“Hello Carl” -creplace “carl”, “eddie”
Hello Carl
第三类i前缀,表示大小写不敏感,和没有前缀的命令逻辑一样(PowerShell中默认的字符串比较是不区分大小写的,所以这里保持一致)。
字符串操作符
操作符
描述
示例
*
代表一个字符串
“PsTips.Net” -like “*”
+
合并两个字符串
“Power” + “Shell”
-replace,-ireplace
替换字符串,大小写不敏感
“PsTips.Net” -replace “tip”,”1″
-creplace
替换字符串,大小写敏感
“PsTips.Net” -replace “Tip”,”1″
-eq, -ieq
验证是否相等,大小写不敏感
“Power” -eq “power”
-ceq
验证是否相等,大小写敏感
“Power” -eq “Power”
-like, -ilike
验证字符串包含关系,允许模式匹配,大小写不敏感
“PsTips.Net” -like “p*”
-clike
验证字符串包含关系,允许模式匹配,大小写敏感
“PsTips.Net” – clike “P*”
-notlike,
-inotlike
验证字符串不包含关系,允许模式匹配,大小写不敏感
“PowerShell” -notlike “PS*”
-cnotlike
验证字符串不包含关系,允许模式匹配,大小写敏感
“PowerShell” -cnotlike “PO*”
-match,-imatch
验证模式匹配,大小写不敏感
“PowerShell” -match “P*”
-cmatch
验证模式匹配,大小写敏感
“Hello” -match “[ao]”
-notmatch,
-inotmatch
验证模式不匹配,大小写不敏感
“Hello” -notmatch “[ao]”
-cnotmatch
验证模式不匹配,大小写敏感
“Hello” -cnotmatch “[ao]”
- PowerShell 使用特殊文本命令 -- 格式化字符串
格式化操作符 –F 能够将一个字符串格式化为指定格式,左边是包含通配符的字符串,右边是待插入和替换的字符串。
“{0} diskettes per CD” -f (720mb/1.44mb)
500 diskettes per CD
-F 右边的表达式必选放在圆括号中,作为一个整体,先进行计算,然后在格式化。否则可能会解析错误:
PS > “{0} diskettes per CD” -f 720mb/1.44mb
数字常量无效: 754974720 diskettes per CD。
所在位置 行:1 字符: 33
+ “{0} diskettes per CD” -f 720mb/ > "GUID with $format : {0}" -f $GUID.ToString($format)}
>>
GUID with N : e1a5d98f4227470b84c2b37a6a8fb894
GUID with D : e1a5d98f-4227-470b-84c2-b37a6a8fb894
GUID with B : {e1a5d98f-4227-470b-84c2-b37a6a8fb894}
GUID with P : (e1a5d98f-4227-470b-84c2-b37a6a8fb894)
- PowerShell 使用特殊文本命令 -- 固定宽度的制表输出
在一个固定宽度和对齐格式中,显示输出多行文本,要求每一列的输出必选具有固定的宽度。格式化操作符可以设置固定宽度输出。
下面的例子通过DIR返回个目录的中的文件列表,然后通过循环输出,文件名和文件大小,因为文件的名字和大小都是不确定的,长度不一样,所以结果拥挤粗糙,可读性差。
PS > dir | ForEach-Object { "$($_.name) = $($_.Length) Bytes" }
.android = Bytes
.VirtualBox = Bytes
CMB = Bytes
Contacts = Bytes
Desktop = Bytes
Documents = Bytes
Downloads = Bytes
Favorites = Bytes
funshion = Bytes
Links = Bytes
Podcasts = Bytes
Roaming = Bytes
Saved Games = Bytes
Searches = Bytes
SkyDrive = Bytes
Tracing = Bytes
Virtual Machines = Bytes
VirtualBox VMs = Bytes
a = 12022 Bytes
a.csv = 986 Bytes
a.ps1 = 18 Bytes
a.txt = 946 Bytes
funshion.ini = 6798 Bytes
PUTTY.RND = 600 Bytes
下面固定列宽的结果,就显得可读性强了。要设置列宽可以将一个逗号放置在通配符与列宽编号的中间,负数设置左对齐{0,-20},左对齐20个字符,取第一个返回值,正数设置右对齐{1,10},右对齐10个字符,取第二个返回值。
PS> dir | ForEach-Object { "{0,-20} = {1,10} Bytes" -f $_.name, $_.Length }
Virtual Machines = Bytes
VirtualBox VMs = Bytes
a = 12022 Bytes
a.csv = 986 Bytes
a.ps1 = 18 Bytes
a.txt = 946 Bytes
funshion.ini = 6798 Bytes
PUTTY.RND = 600 Bytes
.....
- PowerShell 使用特殊文本命令 -- string 对象方法
从之前的章节中,我们知道PowerShell将一切存储在对象中,那这些对象中包含了一系列中的称之为方法的指令。默认文本存储在String对象中,它包含了许多非常有用的处理文本的命令。例如,要确定一个文件的扩展名,可以使用LastIndexOf()获取最后一个字符“.”的位置,继续使用Substring()获取扩展名子串。
PS> $path = "C:\prefs.js"
PS> $path.Substring( $path.LastIndexOf(".")+1 )
Js
另外一条途径,使用Split方法,对文件的完整名称进行分割,得到一个字符串数组,取最后一个元素,PowerShell中可以通过索引-1来获取数组中最后一个元素。
PS> $path.Split(".")[-1]
Js
下面的表格会给出String对象的所有方法:
函数描述示例CompareTo()与另一个字符串比较(“Hello”).CompareTo(“Hello”)Contains()是否包含制定子串(“Hello”).Contains(“ll”)CopyTo()拷贝子串至新字符串中$a = (“HelloWorld”).toCharArray()(“User!”).CopyTo(0, $a, 6, 5)$a
EndsWith()是否以制定子串结尾(“Hello”).EndsWith(“lo”)Equals()是否与另一个字符串相同(“Hello”).Equals($a)IndexOf()返回第一次匹配的所索引(“Hello”).IndexOf(“l”)IndexOfAny()返回字符串中任意字符的首次匹配索引(“Hello”).IndexOfAny(“loe”)Insert()在指定位置插入字符串(“HelloWorld”).Insert(6,”brave “)GetEnumerator()枚举字符串中所有字符(“Hello”).GetEnumerator()LastIndexOf()字符的最后匹配位置(“Hello”).LastIndexOf(“l”)LastIndexOfAny()任意字符的最后匹配位置(“Hello”).LastIndexOfAny(“loe”)PadLeft()左边补齐空白是字符串至指定长度(“Hello”).PadLeft(10)PadRight()右边填充空白是字符串至指定长度(“Hello”).PadRight(10) + “World!”Remove()从指定位置开始移除指定长度(“PsTips”).Remove(2,2)Replace()替换指定字符串(“PsTips”).replace(“Ps”,”PS1″)Split()以指定分隔符切割字符串(“HelloWorld”).Split(“l”)StartsWith()是否以指定子串开始(“HelloWorld”).StartsWith(“He”)Substring()从指定位置取指定长度子串“HelloWorld”).Substring(4,3)ToCharArray()转换成字符数组(“HelloWorld”).toCharArray()ToLower()转换成小写(“HelloWorld”).toLower()ToLowerInvariant ()
以区域规则转换成小写(“HelloWorld”).ToUpperInvariant()ToUpper()转换成大写(“HelloWorld”).ToUpper()ToUpperInvariant ()
以区域规则转换成大写(“HelloWorld”).ToUpperInvariant ()
Trim()移除字符串前后空格(” HelloWorld “). Trim()TrimEnd()移除字符串结尾的空格(“HelloWorld “). TrimEnd()TrimStart()移除字符串开始的空格(” HelloWorld”). TrimStart()Chars()返回指定位置的字符(“Hello”).Chars(0) 以Split()为例来分析方法
在之前的章节中,我们已经知道可以通过Get-Member来查看一个对象中包含了那些可以被调用的方法。正好最为一个简单的回顾,来查看Split的定义。
PS C:\> ("Pstips.net" | Get-Member Split).definition
string[] Split(Params char[] separator), string[] Split(char[] separator, int count), string[] Split(char[] separator, System.StringSplitOptions options), string[] Split(char[] separator, int count, System.StringSplitOptions options), string[] Split(string[] separator, System.StringSplitOptions options), string[] Split(string[] sepa
rator, int count, System.StringSplitOptions options)
Define属性可以获取方法参数定义,但是可读性比较坑爹。我们仍然用上面表格中的Replace方法,将分隔符稍作替换,即可增强可读性。
PS C:\> ("Pstips.net" | Get-Member Split).definition.Replace("), ", ")`n")
string[] Split(Params char[] separator)
string[] Split(char[] separator, int count)
string[] Split(char[] separator, System.StringSplitOptions options)
string[] Split(char[] separator, int count, System.StringSplitOptions options)
string[] Split(string[] separator, System.StringSplitOptions options)
string[] Split(string[] separator, int count, System.StringSplitOptions options)
之前说过反引号,类似高级语言中的转义符反斜杠。
从上面的输出可以发现Split有6种不同的调用方法,而之前可能更多的只使用过一个参数的方法。PowerShell在处理文本时,可能会碰到多个分隔符,而Split方法调用只须一次即可。
PS C:\> "http://www.pstips.net".split(":./")
http
www
pstips
net
中间有空白,咋整,能移除吗,StringSplitOptions轻装上阵:
PS C:\> "http://www.pstips.net".split(":./",[StringSplitOptions]::RemoveEmptyEntries)
http
www
pstips
net
之前有一个小算法题,移除字符串中相邻的重复的空格。在不考虑效率的前提下,可以使用Split先分割,分割后再将得到的元素以指定分隔符拼接。但是拼接用到的Join方法,并不属于string对象,而属于String类,也正是下面要讲的。
- PowerShell 使用特殊文本命令 -- string 类方法
使用String类命令:
之前已经讨论过,对象方法和类方法的区别了,再回顾一次。String对象衍生自string类
在控制台输入[String]::然后按Tab键会自动智能提示,这些方法就是String类命令。
Get-Member会返回所有string对象的方法,可以通过参数只返回静态方法,也就是string类命令。使用几率最高的自然Format方法,但是因为PowerShell中已经有了大书特书的-F操作符了,Format方法可以秒杀了。但是Join和Contac还是可以聊聊的。
Join()方法曾经在上一部分演示Split()提到过,它可以将一个数组或者列表字符串合以指定分隔符并成一个字符串。例如自定义一个函数,移除多余的白空格。
12345function RemoveSpace([string]$text) {$private:array = $text.Split(" ", `[StringSplitOptions]::RemoveEmptyEntries)[string]::Join(" ", $array)}PS C:\> RemoveSpace("PowerShell 中文博客的网址为 :http://www.pstips.net")
PowerShell 中文博客的网址为 :http://www.pstips.net
Concat()将多个字符串拼接成一个字符串。
Concat()工作起来类似字符串操作符“+”,类似而已,总有区别。
PS C:\> "Hello" + " " + "World!"
Hello World!
区别在于第一个左表达式必选是一个String类型,否则,麻烦来了:
PS C:\> (Get-Date) + "PStips.Net"
无法将“op_Addition”的参数“1”(其值为“PStips.Net”)转换为类型“System.TimeSpan”:“无法将值“PStips.Net”转换为类型“System.TimeSpan”。错误:“字符串未被识别为有效
的 TimeSpan。””
所在位置 行:1 字符: 13
+ (Get-Date) + |
|