
- C# 基礎教程
- C# - 首頁
- C# - 概述
- C# - 環境
- C# - 程式結構
- C# - 基本語法
- C# - 資料型別
- C# - 型別轉換
- C# - 變數
- C# - 常量
- C# - 運算子
- C# - 決策制定
- C# - 迴圈
- C# - 封裝
- C# - 方法
- C# - 可空型別
- C# - 陣列
- C# - 字串
- C# - 結構體
- C# - 列舉
- C# - 類
- C# - 繼承
- C# - 多型
- C# - 運算子過載
- C# - 介面
- C# - 名稱空間
- C# - 預處理器指令
- C# - 正則表示式
- C# - 異常處理
- C# - 檔案 I/O
C# - 字串
在 C# 中,您可以將字串用作字元陣列,但是更常見的做法是使用string關鍵字宣告字串變數。string 關鍵字是System.String類的別名。
建立字串物件
您可以使用以下方法之一建立字串物件:
將字串字面量賦值給 String 變數
使用 String 類建構函式
使用字串連線運算子 (+)
檢索返回字串的屬性或呼叫方法
呼叫格式化方法將值或物件轉換為其字串表示形式
以下示例演示了這一點:
using System; namespace StringApplication { class Program { static void Main(string[] args) { //from string literal and string concatenation string fname, lname; fname = "Rowan"; lname = "Atkinson"; char []letters= { 'H', 'e', 'l', 'l','o' }; string [] sarray={ "Hello", "From", "Tutorials", "Point" }; string fullname = fname + lname; Console.WriteLine("Full Name: {0}", fullname); //by using string constructor { 'H', 'e', 'l', 'l','o' }; string greetings = new string(letters); Console.WriteLine("Greetings: {0}", greetings); //methods returning string { "Hello", "From", "Tutorials", "Point" }; string message = String.Join(" ", sarray); Console.WriteLine("Message: {0}", message); //formatting method to convert a value DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1); string chat = String.Format("Message sent at {0:t} on {0:D}", waiting); Console.WriteLine("Message: {0}", chat); } } }
編譯並執行上述程式碼後,將產生以下結果:
Full Name: RowanAtkinson Greetings: Hello Message: Hello From Tutorials Point Message: Message sent at 5:58 PM on Wednesday, October 10, 2012
String 類的屬性
String 類具有以下兩個屬性:
序號 | 屬性和描述 |
---|---|
1 | Chars 獲取當前String物件中指定位置的Char物件。 |
2 | Length 獲取當前 String 物件中的字元數。 |
String 類的方法
String 類有很多方法可以幫助您處理字串物件。下表提供了一些最常用的方法:
序號 | 方法和描述 |
---|---|
1 | public static int Compare(string strA, string strB) 比較兩個指定的字串物件,並返回一個整數,指示它們在排序順序中的相對位置。 |
2 | public static int Compare(string strA, string strB, bool ignoreCase ) 比較兩個指定的字串物件,並返回一個整數,指示它們在排序順序中的相對位置。但是,如果布林引數為 true,則忽略大小寫。 |
3 | public static string Concat(string str0, string str1) 連線兩個字串物件。 |
4 | public static string Concat(string str0, string str1, string str2) 連線三個字串物件。 |
5 | public static string Concat(string str0, string str1, string str2, string str3) 連線四個字串物件。 |
6 | public bool Contains(string value) 返回一個值,指示指定的 String 物件是否在此字串中出現。 |
7 | public static string Copy(string str) 建立一個新的 String 物件,其值與指定的字串相同。 |
8 | public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count) 將指定數量的字元從 String 物件的指定位置複製到 Unicode 字元陣列的指定位置。 |
9 | public bool EndsWith(string value) 確定字串物件的末尾是否與指定的字串匹配。 |
10 | public bool Equals(string value) 確定當前 String 物件和指定的 String 物件的值是否相同。 |
11 | public static bool Equals(string a, string b) 確定兩個指定的 String 物件的值是否相同。 |
12 | public static string Format(string format, Object arg0) 將指定字串中一個或多個格式項替換為指定物件的字串表示形式。 |
13 | public int IndexOf(char value) 返回當前字串中指定 Unicode 字元第一次出現的零基索引。 |
14 | public int IndexOf(string value) 返回此例項中指定字串第一次出現的零基索引。 |
15 | public int IndexOf(char value, int startIndex) 返回此字串中指定 Unicode 字元第一次出現的零基索引,從指定的字元位置開始搜尋。 |
16 | public int IndexOf(string value, int startIndex) 返回此例項中指定字串第一次出現的零基索引,從指定的字元位置開始搜尋。 |
17 | public int IndexOfAny(char[] anyOf) 返回此例項中指定 Unicode 字元陣列中任何字元第一次出現的零基索引。 |
18 | public int IndexOfAny(char[] anyOf, int startIndex) 返回此例項中指定 Unicode 字元陣列中任何字元第一次出現的零基索引,從指定的字元位置開始搜尋。 |
19 | public string Insert(int startIndex, string value) 返回一個新字串,其中指定的字串插入到當前字串物件的指定索引位置。 |
20 | public static bool IsNullOrEmpty(string value) 指示指定的字串是否為 null 或空字串。 |
21 | public static string Join(string separator, params string[] value) 使用指定的每個元素之間的分隔符連線字串陣列的所有元素。 |
22 | public static string Join(string separator, string[] value, int startIndex, int count) 使用指定的每個元素之間的分隔符連線字串陣列的指定元素。 |
23 | public int LastIndexOf(char value) 返回當前字串物件中指定 Unicode 字元最後一次出現的零基索引位置。 |
24 | public int LastIndexOf(string value) 返回當前字串物件中指定字串最後一次出現的零基索引位置。 |
25 | public string Remove(int startIndex) 刪除當前例項中從指定位置開始到最後位置的所有字元,並返回字串。 |
26 | public string Remove(int startIndex, int count) 刪除當前字串中從指定位置開始的指定數量的字元,並返回字串。 |
27 | public string Replace(char oldChar, char newChar) 將當前字串物件中指定 Unicode 字元的所有出現替換為指定的 Unicode 字元,並返回新字串。 |
28 | public string Replace(string oldValue, string newValue) 將當前字串物件中指定字串的所有出現替換為指定的字串,並返回新字串。 |
29 | public string[] Split(params char[] separator) 返回一個字串陣列,其中包含當前字串物件中的子字串,由指定 Unicode 字元陣列的元素分隔。 |
30 | public string[] Split(char[] separator, int count) 返回一個字串陣列,其中包含當前字串物件中的子字串,由指定 Unicode 字元陣列的元素分隔。int 引數指定要返回的子字串的最大數量。 |
31 | public bool StartsWith(string value) 確定此字串例項的開頭是否與指定的字串匹配。 |
32 | public char[] ToCharArray() 返回一個包含當前字串物件中所有字元的 Unicode 字元陣列。 |
33 | public char[] ToCharArray(int startIndex, int length) 返回一個包含當前字串物件中所有字元的 Unicode 字元陣列,從指定的索引開始到指定的長度。 |
34 | public string ToLower() 返回轉換為小寫的此字串的副本。 |
35 | public string ToUpper() 返回轉換為大寫的此字串的副本。 |
36 | public string Trim() 刪除當前 String 物件中所有前導和尾隨的空格字元。 |
您可以訪問 MSDN 庫以獲取方法和 String 類建構函式的完整列表。
示例
以下示例演示了上面提到的一些方法:
比較字串
using System; namespace StringApplication { class StringProg { static void Main(string[] args) { string str1 = "This is test"; string str2 = "This is text"; if (String.Compare(str1, str2) == 0) { Console.WriteLine(str1 + " and " + str2 + " are equal."); } else { Console.WriteLine(str1 + " and " + str2 + " are not equal."); } Console.ReadKey() ; } } }
編譯並執行上述程式碼後,將產生以下結果:
This is test and This is text are not equal.
字串包含字串
using System; namespace StringApplication { class StringProg { static void Main(string[] args) { string str = "This is test"; if (str.Contains("test")) { Console.WriteLine("The sequence 'test' was found."); } Console.ReadKey() ; } } }
編譯並執行上述程式碼後,將產生以下結果:
The sequence 'test' was found.
獲取子字串
using System; namespace StringApplication { class StringProg { static void Main(string[] args) { string str = "Last night I dreamt of San Pedro"; Console.WriteLine(str); string substr = str.Substring(23); Console.WriteLine(substr); } } }
編譯並執行上述程式碼後,將產生以下結果:
San Pedro
連線字串
using System; namespace StringApplication { class StringProg { static void Main(string[] args) { string[] starray = new string[]{"Down the way nights are dark", "And the sun shines daily on the mountain top", "I took a trip on a sailing ship", "And when I reached Jamaica", "I made a stop"}; string str = String.Join("\n", starray); Console.WriteLine(str); } } }
編譯並執行上述程式碼後,將產生以下結果:
Down the way nights are dark And the sun shines daily on the mountain top I took a trip on a sailing ship And when I reached Jamaica I made a stop