從之前的章節(jié)中,我們知道PowerShell將一切存儲(chǔ)在對(duì)象中,那這些對(duì)象中包含了一系列中的稱之為方法的指令。默認(rèn)文本存儲(chǔ)在String對(duì)象中,它包含了許多非常有用的處理文本的命令。例如,要確定一個(gè)文件的擴(kuò)展名,可以使用LastIndexOf()獲取最后一個(gè)字符“.”的位置,繼續(xù)使用Substring()獲取擴(kuò)展名子串。
1
2
3
|
PS > $path = "C:prefs.js" PS > $path .Substring( $path .LastIndexOf( "." )+1 ) Js |
另外一條途徑,使用Split方法,對(duì)文件的完整名稱進(jìn)行分割,得到一個(gè)字符串?dāng)?shù)組,取最后一個(gè)元素,PowerShell中可以通過(guò)索引-1來(lái)獲取數(shù)組中最后一個(gè)元素。
1
2
|
PS > $path .Split( "." )[-1] Js |
下面的表格會(huì)給出String對(duì)象的所有方法:
函數(shù) | 描述 | 示例 |
CompareTo() | 與另一個(gè)字符串比較 | (“Hello”).CompareTo(“Hello”) |
Contains() | 是否包含制定子串 | (“Hello”).Contains(“ll”) |
CopyTo() | 拷貝子串至新字符串中 |
$a = (“HelloWorld”).toCharArray()(“User!”).CopyTo(0,
$a, 6, 5)$a |
EndsWith() | 是否以制定子串結(jié)尾 | (“Hello”).EndsWith(“lo”) |
Equals() | 是否與另一個(gè)字符串相同 | (“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() | 左邊補(bǔ)齊空白是字符串至指定長(zhǎng)度 | (“Hello”).PadLeft(10) |
PadRight() | 右邊填充空白是字符串至指定長(zhǎng)度 | (“Hello”).PadRight(10) + “World!” |
Remove() | 從指定位置開始移除指定長(zhǎng)度 | (“PsTips”).Remove(2,2) |
Replace() | 替換指定字符串 | (“PsTips”).replace(“Ps”,”PS1″) |
Split() | 以指定分隔符切割字符串 | (“HelloWorld”).Split(“l”) |
StartsWith() | 是否以指定子串開始 | (“HelloWorld”).StartsWith(“He”) |
Substring() | 從指定位置取指定長(zhǎng)度子串 | “HelloWorld”).Substring(4,3) |
ToCharArray() | 轉(zhuǎn)換成字符數(shù)組 | (“HelloWorld”).toCharArray() |
ToLower() | 轉(zhuǎn)換成小寫 | (“HelloWorld”).toLower() |
ToLowerInvariant
() |
以區(qū)域規(guī)則轉(zhuǎn)換成小寫 | (“HelloWorld”).ToUpperInvariant() |
ToUpper() | 轉(zhuǎn)換成大寫 | (“HelloWorld”).ToUpper() |
ToUpperInvariant
() |
以區(qū)域規(guī)則轉(zhuǎn)換成大寫 |
(“HelloWorld”).ToUpperInvariant
() |
Trim() | 移除字符串前后空格 | (” HelloWorld “). Trim() |
TrimEnd() | 移除字符串結(jié)尾的空格 | (“HelloWorld “). TrimEnd() |
TrimStart() | 移除字符串開始的空格 | (” HelloWorld”). TrimStart() |
Chars() | 返回指定位置的字符 | (“Hello”).Chars(0) |
以Split()為例來(lái)分析方法
在之前的章節(jié)中,我們已經(jīng)知道可以通過(guò)Get-Member來(lái)查看一個(gè)對(duì)象中包含了那些可以被調(diào)用的方法。正好最為一個(gè)簡(jiǎn)單的回顧,來(lái)查看Split的定義。
1
2
3
|
PS C:> ( "zzvips.com" | 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屬性可以獲取方法參數(shù)定義,但是可讀性比較坑爹。我們?nèi)匀挥蒙厦姹砀裰械腞eplace方法,將分隔符稍作替換,即可增強(qiáng)可讀性。
1
2
3
4
5
6
7
|
PS C:> ( "zzvips.com" | 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) |
之前說(shuō)過(guò)反引號(hào),類似高級(jí)語(yǔ)言中的轉(zhuǎn)義符反斜杠。
從上面的輸出可以發(fā)現(xiàn)Split有6種不同的調(diào)用方法,而之前可能更多的只使用過(guò)一個(gè)參數(shù)的方法。PowerShell在處理文本時(shí),可能會(huì)碰到多個(gè)分隔符,而Split方法調(diào)用只須一次即可。
1
2
3
4
5
6
|
中間有空白,咋整,能移除嗎,StringSplitOptions輕裝上陣:
1
2
3
4
5
|
http www pstips net |
之前有一個(gè)小算法題,移除字符串中相鄰的重復(fù)的空格。在不考慮效率的前提下,可以使用Split先分割,分割后再將得到的元素以指定分隔符拼接。但是拼接用到的Join方法,并不屬于string對(duì)象,而屬于String類,也正是下面要講的。
Text and Regular Expressions