1. 程式人生 > >C#、字元、字串

C#、字元、字串

一、Char?

1.1Char在C#中表示一個Unicode字元,正是這些Unicode字元構成了字串。Unicode字元是目前計算機通用的字元編碼,它為針對不同的語言的每個字元設定了統一的二進位制編碼,用於滿足跨語言,跨平臺的文字轉換,處理的要求。Char只能定義一個Unicode字元。

1.2 Char常用方法:(以Is和To開頭的為重要方法。Is開頭的方法大多是判斷Unicode字元是否為某個類別,以To開頭的方法主要是轉換為其他Unicode字元。)

     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);   //即:xyzdef2.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);  //即:abcdf3.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);  //即:cd4.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);  //即:17.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);//即:true8.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不填則全部拆分

9.Contains(判斷是否存在) public bool Contains(string text):如果字串中出現text,則返回true,反之false,如果text為("")也返回true。 如:  string st="語文數學英語";  bool b=st.Contains("語文");  Console.WriteLine(b);//true10.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

轉義字元

#採用字元”\”作為轉義字元。

C

String?

1.1字串的關鍵字為string。

它是String類的別名。string型別表示Unicode字元的字串。String類類似於string型別,但是功能更強。雖然String類功能很強,但是它也是不可改變的。這就是說一旦建立String物件,就不能夠修改。表面看來能夠修改字串的所有方法,實際上不能夠修改。它們實際上返回一個根據所呼叫的方法修改的新的String。當需要大量的修改時,可使用StringBuilder類。

1.2比較字串

比較字串並非比較字串長度的大小,而是比較字串在英文字典中的位置。比較字串按照字典排序的規則,判斷;兩個字串的大小。在英文字典中,前面的單詞小於後面的單詞。

Compare方法用來比較兩個字串是否相等。

CompareTo方法用來比較兩個字元是否相等,不同的是CompareTo方法以例項物件本身與指定的字串比較。

Equals方法主要用於比較兩個字串是否相等,如果相同返回值為true,否則為false。

1.1擷取字串

SubString方法,該方法可以擷取字串中指定位置和指定長度的字元。

1.2分割字串

Split方法,用於分割字串。該方法的返回值是包含所有分割子字串的陣列物件,可以通過陣列取得所有分割的子字串。

1.3插入字串

Insert方法,用於向字串的任意位置插入新元素。

1.4填充字串

PadLeft/PadRight 填充字元。

1.5刪除字串

Remove方法,用於從一個字串指定位置開始,刪除指定數量的字元。

1.6複製字串

Copy和CopyTo方法,用於將字串複製到另外一個字串或char型別的陣列中。

1.7替換字串

Replace方法,用於將字串中的某個字元或者字串換成其他的字元或者字串。