[C#]C#中字串的操作

阿婆說發表於2018-06-23

1.Replace(替換字元):
public string Replace(char oldChar,char newChar);在物件中尋找oldChar,如果尋找到,就用newChar將oldChar替換掉。
如:
            string st = “abcdef”;
            string newstring = st.Replace(`a`, `x`);
            Console.WriteLine(newstring);   //即:xbcdef

public string Replace(string oldString,string newString);在物件中尋找oldString,如果尋找到,就用newString將oldString替換掉。
如:
            string st = “abcdef”;
            string newstring = st.Replace(“abc”, “xyz”);
            Console.WriteLine(newstring);   //即:xyzdef

2.Remove(刪除字元):
public string Remove(int startIndex);從startIndex位置開始,刪除此位置後所有的字元(包括當前位置所指定的字元)。
如:  
     string st = “abcdef”;
            string newstring = st.Remove(4);
            Console.WriteLine(newstring);  //即:abcd

public string Remove(int startIndex,int count);從startIndex位置開始,刪除count個字元。
如:  
     string st = “abcdef”;
            string newstring = st.Remove(4,1);
            Console.WriteLine(newstring);  //即:abcdf

3.Substring(字串提取):
public string Substring(int startIndex);從startIndex位置開始,提取此位置後所有的字元(包括當前位置所指定的字元)。
如:  
     string st = “abcdef”;
            string newstring = st.Substring(2);
            Console.WriteLine(newstring);  //即:cdef

public string Substring(int startIndex,int count);從startIndex位置開始,提取count個字元。
如:  
     string st = “abcdef”;
            string newstring = st.Substring(2,2);
            Console.WriteLine(newstring);  //即:cd

4.Trim(清空空格):
public string Trim ():將字串物件包含的字串兩邊的空格去掉後返回。
public string Trim ( params char[] trimChars ):從此例項的開始和末尾移除陣列中指定的一組字元的所有匹配項。
如:
     string st =”abcdef”;
     string newstring = st.Trim(new char[] {`a`});//尋找st字串中開始與末尾是否有與`a`匹配,如有,將其移除。
     Console.WriteLine(newstring); //即:bcdef
注:如果字串為”aaaabcdef”,返回依然為bcdef。當移除第一個a時,開始依然為a,繼續移除,直到沒有。
public string TrimEnd ( params char[] trimChars ):對此例項末尾與指定字元進行匹配,true則移除
public string TrimStart ( params char[] trimChars ):對此例項開始與指定字元進行匹配,true則移除

5.ToLower(轉換大小寫)

public string ToLower():將字串物件包含的字串中的大寫全部轉換為小寫。

6.IndexOf(獲取指定的字串的開始索引)
public int IndexOf (sring field):在此例項中尋找field,如果尋找到,返回開始索引,反之,返回-1。
如:
       string st = “abcdef”;
            int num=st.IndexOf(“bcd”);
            Console.WriteLine(num);  //即:1

7.Equals(是否相等)
public bool Equals (string value):比較呼叫方法的字串物件包含字串和引數給出的物件是否相同,如相同,就返回true,反之,返回false。
如:        string a = “abcdef”;
            bool b = a.Equals(“bcdef”);
            Console.WriteLine(b);//即:false

public bool Equals ( string value, StringComparison comparisonType ):比較呼叫方法的字串物件包含字串和引數給出的物件是否在不區分大小寫的情況下相同,如相同,就返回true,反之,返回false,第二個引數將指定區域性、大小寫以及比較所用的排序規則.
如:
       string a = “ABCDEF”;
            bool b = a.Equals(“abcdef”,StringComparison.CurrentCultureIgnoreCase);
            Console.WriteLine(b);//即:true

8.Split(拆分)
public string[] Split ( params char[] separator ):根據separator 指定的沒有字元分隔此例項中子字串成為Unicode字元陣列, separator可以是不包含分隔符的空陣列或空引用。
public string[] Split ( char[] separator, int count ):引數count 指定要返回的子字串的最大數量。 
如:
            string st = “語文|數學|英語|物理”;
            string[] split = st.Split(new char[]{`|`},2);
            for (int i = 0; i < split.Length; i++)
            {
                Console.WriteLine(split[i]);
            }
注:count不填則全部拆分

public enum StringSplitOptions 
成員名稱            說明
None                返回值包括含有空字串的陣列元素
RemoveEmptyEntries  返回值不包括含有空字串的陣列元素

如:
            string st = “語文|數學||英語|物理”;
            string[] split = st.Split(new char[]{`|`},StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < split.Length; i++)
            {
                Console.WriteLine(split[i]);
            }
將StringSplitOptions列舉和Split()方法聯絡起來:
1.  public string[] Split ( char[] separator, StringSplitOptions options ):options指定StringSplitOptions列舉的RemoveEmptyEntries以省略返回的陣列中的空陣列元素,或指定StringSplitOptions列舉的None以包含返回的陣列中的空陣列元
2.  public string[] Split ( char[] separator, int count, StringSplitOptions options ) 
3.  public string[] Split ( string[] separator, StringSplitOptions options ) 
4.  public string[] Split ( string[] separator, int count, StringSplitOptions options )

9.Contains(判斷是否存在)
public bool Contains(string text):如果字串中出現text,則返回true,反之false,如果text為(“”)也返回true。
如:
 string st=”語文數學英語”;
 bool b=st.Contains(“語文”);
 Console.WriteLine(b);//true

10.EndsWith,StartsWith(判斷字串的開始或結束)
public bool EndsWith ( string value ):判斷物件包含字串是否以value指定的字串結束,是則為 true;否則為 false。 
public bool EndsWith ( string value, StringComparison comparisonType ):第二個引數設定比較時區域、大小寫和排序規則。
public bool StartsWith ( string value ):判斷物件包含字串是否以value指定的字串開始,是則為 true;否則為 false。 
public bool StartsWith ( string value, StringComparison comparisonType ) :第二個引數設定比較時區域、大小寫和排序規則。
如:
 string st=”語文數學英語abc”;
 bool b=st.EndsWith(“英語ABC”,StringComparison.CurrentCultureIgnoreCase);//第二個引數忽略大小比較。
 Console.WriteLine(b);//true

11.Insert(字串插入)
public string Insert ( int startIndex, string value ):在指定的字串下標為startIndex前插入字串value。返回插入後的值。
如:
 string st=”語文數學英語abc”;
 string newst=st.Insert(6,”物理”);//注:在指定索引“前”插入。
 Console.WriteLine(newst);//即:語文數學英語物理abc

相關文章