標籤:c#
理論:
C# 字串(String)
在 C# 中,您可以使用字元數組來表示字串,但是,更常見的做法是使用 string 關鍵字來聲明一個字串變數。
string 關鍵字是 System.String 類的別名。
建立 String 對象 您可以使用以下方法之一來建立 string 對象:
通過給 String 變數指定一個字串
通過使用 String 類建構函式
通過使用字串串聯運算子( + )
通過檢索屬性或調用一個返回字元..
String 類的屬性
String 類有以下兩個屬性:
1. Chars 在當前 String 對象中擷取 Char 對象的指定位置。
2. Length 在當前的 String 對象中擷取字元數。
String 類的方法
String 類有許多方法用於 string 對象的操作。下面的表格提供了一些最常用的方法:
650) this.width=650;" title="Capture.PNG" src="http://s3.51cto.com/wyfs02/M02/6D/D8/wKiom1VtI7DxX4T4AASYCto9pyM742.jpg" alt="wKiom1VtI7DxX4T4AASYCto9pyM742.jpg" />
650) this.width=650;" title="Capture.2PNG.PNG" src="http://s3.51cto.com/wyfs02/M01/6D/D8/wKiom1VtI53wycNJAAWpNfH0m4M059.jpg" alt="wKiom1VtI53wycNJAAWpNfH0m4M059.jpg" />
650) this.width=650;" title="Capture3.PNG" src="http://s3.51cto.com/wyfs02/M00/6D/D8/wKiom1VtI8Syh9c-AAHIyNx8SFw886.jpg" alt="wKiom1VtI8Syh9c-AAHIyNx8SFw886.jpg" />
執行個體1:
using System;namespace StringApplication{ class Program { static void Main(string[] args) { //字串,字串串連 string fname, lname; fname = "Rowan"; lname = "Atkinson"; string fullname = fname + lname; Console.WriteLine("Full Name: {0}", fullname); //通過使用 string 建構函式 char[] letters = { ‘H‘, ‘e‘, ‘l‘, ‘l‘,‘o‘ }; string greetings = new string(letters); Console.WriteLine("Greetings: {0}", greetings); //方法返回字串 string[] sarray = { "Hello", "From", "Tutorials", "Point" }; string message = String.Join(" ", sarray); Console.WriteLine("Message: {0}", message); //用於轉化值的格式化方法 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); Console.ReadKey() ; } }}//當上面的代碼被編譯和執行時,它會產生下列結果://Full Name: Rowan Atkinson//Greetings: Hello//Message: Hello From Tutorials Point//Message: Message sent at 5:58 PM on Wednesday, October 10, 2012
執行個體2:
下面的執行個體示範了上面提到的一些方法:
比較字串
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); } Console.ReadKey() ; }}//當上面的代碼被編譯和執行時,它會產生下列結果: 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); } Console.ReadKey() ; }}
當上面的代碼被編譯和執行時,它會產生下列結果:
Down the way nights are darkAnd the sun shines daily on the mountain topI took a trip on a sailing shipAnd when I reached JamaicaI made a stop
參考:
http://outofmemory.cn/csharp/tutorial/csharp-string.html
本文出自 “Ricky's Blog” 部落格,請務必保留此出處http://57388.blog.51cto.com/47388/1657426
57. C# -- 字串(string)