加號 + 可用於字串的連接,舉例如下
class Demo {
static void Main() {
string a = "Hello,";
System.Console.WriteLine(a);
string b = " world!";
System.Console.WriteLine(b);
string c = a + b;
System.Console.WriteLine(c);
}
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:string1.cs
功能:示範 C# 程式
作者:張凱慶
時間:西元 2013 年 6 月 */編譯執行,結果如下

利用中括弧 [] 可以存取字串中字元,例如
class Demo {
static void Main() {
string a = "Hello";
int i = 0;
while (i < 5) {
System.Console.WriteLine(a[i]);
i++;
}
}
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:string2.cs
功能:示範 C# 程式
作者:張凱慶
時間:西元 2013 年 6 月 */編譯執行,結果如下

有 @ 前綴的字串裡頭的跳脫字元不會被處理,例如
class Demo {
static void Main() {
string a = "H\tello";
System.Console.WriteLine(a);
string b = @"H\tello";
System.Console.WriteLine(b);
}
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:string3.cs
功能:示範 C# 程式
作者:張凱慶
時間:西元 2013 年 6 月 */編譯執行,結果如下

| 中英文術語對照 | |
|---|---|
| 關鍵字 | keyword |
| 類別 | class |
| 字串 | string |
| 字面常數 | literal |
| 字元 | character |
您可以繼續參考
其他內建型態
相關目錄
回 C# 快速導覽
回 C# 教材
回首頁
參考資料
Standard ECMA-334 C# Language Specification
msdn: string (C# 參考)
沒有留言:
張貼留言