PowerShell String對象方法小結(jié)
從之前的章節(jié)中,我們知道PowerShell將一切存儲在對象中,那這些對象中包含了一系列中的稱之為方法的指令。默認文本存儲在String對象中,它包含了許多非常有用的處理文本的命令。例如,要確定一個文件的擴展名,可以使用LastIndexOf()獲取最后一個字符“.”的位置,繼續(xù)使用Substring()獲取擴展名子串。
PS> $path = "C:\prefs.js"
PS> $path.Substring( $path.LastIndexOf(".")+1 )
Js
另外一條途徑,使用Split方法,對文件的完整名稱進行分割,得到一個字符串?dāng)?shù)組,取最后一個元素,PowerShell中可以通過索引-1來獲取數(shù)組中最后一個元素。
PS> $path.Split(".")[-1]
Js
下面的表格會給出String對象的所有方法:
| 函數(shù) | 描述 | 示例 |
| CompareTo() | 與另一個字符串比較 | (“Hello”).CompareTo(“Hello”) |
| Contains() | 是否包含制定子串 | (“Hello”).Contains(“l(fā)l”) |
| CopyTo() | 拷貝子串至新字符串中 | $a = (“HelloWorld”).toCharArray()(“User!”).CopyTo(0,
$a, 6, 5)$a |
| EndsWith() | 是否以制定子串結(jié)尾 | (“Hello”).EndsWith(“l(fā)o”) |
| Equals() | 是否與另一個字符串相同 | (“Hello”).Equals($a) |
| IndexOf() | 返回第一次匹配的所索引 | (“Hello”).IndexOf(“l(fā)”) |
| IndexOfAny() | 返回字符串中任意字符的首次匹配索引 | (“Hello”).IndexOfAny(“l(fā)oe”) |
| Insert() | 在指定位置插入字符串 | (“HelloWorld”).Insert(6,”brave “) |
| GetEnumerator() | 枚舉字符串中所有字符 | (“Hello”).GetEnumerator() |
| LastIndexOf() | 字符的最后匹配位置 | (“Hello”).LastIndexOf(“l(fā)”) |
| LastIndexOfAny() | 任意字符的最后匹配位置 | (“Hello”).LastIndexOfAny(“l(fā)oe”) |
| PadLeft() | 左邊補齊空白是字符串至指定長度 | (“Hello”).PadLeft(10) |
| PadRight() | 右邊填充空白是字符串至指定長度 | (“Hello”).PadRight(10) + “World!” |
| Remove() | 從指定位置開始移除指定長度 | (“PsTips”).Remove(2,2) |
| Replace() | 替換指定字符串 | (“PsTips”).replace(“Ps”,”PS1″) |
| Split() | 以指定分隔符切割字符串 | (“HelloWorld”).Split(“l(fā)”) |
| StartsWith() | 是否以指定子串開始 | (“HelloWorld”).StartsWith(“He”) |
| Substring() | 從指定位置取指定長度子串 | “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()為例來分析方法
在之前的章節(jié)中,我們已經(jīng)知道可以通過Get-Member來查看一個對象中包含了那些可以被調(diào)用的方法。正好最為一個簡單的回顧,來查看Split的定義。
PS C:\> ("jb51.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屬性可以獲取方法參數(shù)定義,但是可讀性比較坑爹。我們?nèi)匀挥蒙厦姹砀裰械腞eplace方法,將分隔符稍作替換,即可增強可讀性。
PS C:\> ("jb51.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)
之前說過反引號,類似高級語言中的轉(zhuǎn)義符反斜杠。
從上面的輸出可以發(fā)現(xiàn)Split有6種不同的調(diào)用方法,而之前可能更多的只使用過一個參數(shù)的方法。PowerShell在處理文本時,可能會碰到多個分隔符,而Split方法調(diào)用只須一次即可。
PS C:\> "http://www.dhdzp.com".split(":./")
http
www
pstips
net
中間有空白,咋整,能移除嗎,StringSplitOptions輕裝上陣:
PS C:\> "http://www.dhdzp.com".split(":./",[StringSplitOptions]::RemoveEmptyEntries)
http
www
pstips
net
之前有一個小算法題,移除字符串中相鄰的重復(fù)的空格。在不考慮效率的前提下,可以使用Split先分割,分割后再將得到的元素以指定分隔符拼接。但是拼接用到的Join方法,并不屬于string對象,而屬于String類,也正是下面要講的。
原文: http://www.dhdzp.com/string-object-methods.html
相關(guān)文章
PowerShell把IP地址轉(zhuǎn)換成二進制的方法
這篇文章主要介紹了PowerShell把IP地址轉(zhuǎn)換成二進制的方法,在一些IP判斷的場合經(jīng)常使用的小技巧,需要的朋友可以參考下2014-08-08
PowerShell中使用curl(Invoke-WebRequest)的方法教程
這篇文章主要給大家介紹了關(guān)于在PowerShell中使用curl(Invoke-WebRequest)的方法教程,文中通過詳細的示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-08-08
Powershell小技巧之獲取當(dāng)前的時間并轉(zhuǎn)換為時辰
這篇文章主要介紹了使用Powershell獲取當(dāng)前的時間并轉(zhuǎn)換為時辰的方法,非常簡單實用,有需要的朋友可以參考下2014-09-09
PowerShell顯示隱藏文件和系統(tǒng)文件的方法
這篇文章主要介紹了PowerShell顯示隱藏文件和系統(tǒng)文件的方法,重點在于對文件、文件夾屬性的介紹,需要的朋友可以參考下2014-08-08
PowerShell中把相對路徑轉(zhuǎn)換為絕對路徑的2個方法
這篇文章主要介紹了PowerShell中把相對路徑轉(zhuǎn)換為絕對路徑的2個方法,并對他的區(qū)別做了講解,需要的朋友可以參考下2014-08-08

