天天看點

常用字元串處理函數

方法名

描述

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.compare(vs.80).aspx">compare</a>

比較兩個指定的 string 對象。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.compareordinal(vs.80).aspx">compareordinal</a>

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.compareto(vs.80).aspx">compareto</a>

将此執行個體與指定的對象或 string 進行比較,并傳回二者相對值的訓示。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.concat(vs.80).aspx">concat</a>

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/dy85x1sa(vs.80).aspx">contains</a>

傳回一個值,該值訓示指定的 string 對象是否出現在此字元串中。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.copy(vs.80).aspx">copy</a>

建立一個與指定的 string 具有相同值的 string 的新執行個體。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.copyto(vs.80).aspx">copyto</a>

将指定數目的字元從此執行個體中的指定位置複制到 unicode 字元數組中的指定位置。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.endswith(vs.80).aspx">endswith</a>

确定 string 的執行個體的末尾是否與指定的字元串比對。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.getenumerator(vs.80).aspx">getenumerator</a>

檢索一個可以循環通路此字元串中的每個字元的對象。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.indexof(vs.80).aspx">indexof</a>

報告 string 或一個或多個字元在此字元串中的第一個比對項的索引。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.indexofany(vs.80).aspx">indexofany</a>

報告指定 unicode 字元數組中的任意字元在此執行個體中第一個比對項的索引。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.insert(vs.80).aspx">insert</a>

在此執行個體中的指定索引位置插入一個指定的 string 執行個體。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.intern(vs.80).aspx">intern</a>

檢索系統對指定 string 的引用。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.isinterned(vs.80).aspx">isinterned</a>

檢索對指定 string 的引用。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.join(vs.80).aspx">join</a>

在指定 string 數組的每個元素之間串聯指定的分隔符 string,進而産生單個串聯的字元串。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.lastindexof(vs.80).aspx">lastindexof</a>

報告指定的 unicode 字元或 string 在此執行個體中的最後一個比對項的索引位置。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.lastindexofany(vs.80).aspx">lastindexofany</a>

報告在 unicode 數組中指定的一個或多個字元在此執行個體中的最後一個比對項的索引位置。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.normalize(vs.80).aspx">normalize</a>

傳回一個新字元串,其二進制表示形式符合特定的 unicode 範式。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.padleft(vs.80).aspx">padleft</a>

右對齊此執行個體中的字元,在左邊用空格或指定的 unicode 字元填充以達到指定的總長度。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.padright(vs.80).aspx">padright</a>

左對齊此字元串中的字元,在右邊用空格或指定的 unicode 字元填充以達到指定的總長度。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.remove(vs.80).aspx">remove</a>

從此執行個體中删除指定個數的字元。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.replace(vs.80).aspx">replace</a>

将此執行個體中的指定 unicode 字元或 string 的所有比對項替換為其他指定的 unicode 字元或string。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.split(vs.80).aspx">split</a>

傳回包含此執行個體中的子字元串(由指定 char 或 string 數組的元素分隔)的 string 數組。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.substring(vs.80).aspx">substring</a>

從此執行個體檢索子字元串。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.startswith(vs.80).aspx">startswith</a>

确定 string 執行個體的開頭是否與指定的字元串比對。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.tochararray(vs.80).aspx">tochararray</a>

将此執行個體中的字元複制到 unicode 字元數組。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.tolower(vs.80).aspx">tolower</a>

傳回此 string 對象的轉換為小寫形式的副本,傳回時使用固定區域性的大小寫規則。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.tolowerinvariant(vs.80).aspx">tolowerinvariant</a>

将此執行個體的值轉換為 string。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.toupper(vs.80).aspx">toupper</a>

傳回此 string 轉換為大寫形式的副本。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.toupperinvariant(vs.80).aspx">toupperinvariant</a>

傳回此 string 對象的轉換為大寫形式的副本,傳回時使用固定區域性的大小寫規則。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.trim(vs.80).aspx">trim</a>

從此執行個體的開始位置和末尾移除一組指定字元的所有比對項。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.trimend(vs.80).aspx">trimend</a>

從此執行個體的結尾移除數組中指定的一組字元的所有比對項。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.trimstart(vs.80).aspx">trimstart</a>

從此執行個體的開始位置移除數組中指定的一組字元的所有比對項。

以下進行部分常用函數舉例

1)compare函數:比較兩個字元串的大小(前大為1,相同偉0,後大為-1)

程式運作結果:-1

2)compareordinal函數:逐個比較兩個字元串的各個字元,發現不同,停止比較,并傳回兩者的ascii碼之差(如果相同則為0)

程式運作結果:-32

3)compareto函數:執行個體函數,比較兩個字元串的大小(前大為1,相同偉0,後大為-1)

程式運作結果:1

4)endswith函數:執行個體函數,确定一個執行個體的末尾與指定的字元串是否比對

程式運作結果:false

5)indexof函數:執行個體函數,從執行個體的指定位置查找執行個體中第一個與指定字元串比對的字元串,如果找到則傳回位置,找不到傳回-1

程式運作結果:2

6)insert函數:執行個體函數,在執行個體的指定位置開始插入指定的字元串,傳回最終的字元串

程式運作結果:once upon a time

7)join函數,在字元串數組的每個元素之間插入指定的分隔符

程式運作結果:hello and welcome to my world!

8)lastindexof函數:執行個體函數,從執行個體的指定位置查找執行個體中最後一個與指定字元串比對的字元串,如果找到則傳回位置,找不到傳回-1

程式運作結果:9

9)padleft函數:執行個體函數,将執行個體按指定長度填充滿,左邊不足部分用指定字元替代

程式運作結果:--------hello world!

此程式使用padleft方法來建立一個總長度為20個空間的新字元串。

10)remove函數:執行個體函數,将指定字元串執行個體的指定位置删除指定數目的字元

string mystring = "hello beautifulworld!";

console.writeline(mystring.remove(5,10));

程式運作結果:hello world!

從字元串的從零開始的索引的第5個位置開始,從該字元串中移除10個字元。

11)startswith函數:執行個體函數,确定一個執行個體的開頭與指定的字元串是否比對

程式運作結果:true

12)substring函數:執行個體函數,從字元串中的指定位置提取指定數目的子字元串

程式運作結果:world

13)tolower函數:執行個體函數,字元串全部變小寫

14)trimstart函數:執行個體函數

程式運作結果:world!

15)trimend函數:執行個體函數

程式運作結果:he

在此例子中,‘r’字元和‘w’字元的位置反轉,對于運作結果沒有影響。

程式運作結果:hello,

在此例子中,單詞hello後尾随一個逗号,而由于在要移除的字元的數組中沒有指定逗号,是以移除在逗号處結束。

16)indexofany函數和lastindexofany函數:執行個體函數

程式運作結果:0

程式運作結果:3

17)split函數:執行個體函數

程式運作結果:

堅持

可以創造奇迹

不幸的是

很少有人

能夠長時間地堅持下去

直至

奇迹發生

繼續閱讀