在這里我們先找補一下命名空間的概念,這個對於我們的類的概念十分重要。
namespace命名空間:用於解決重名問題,可以看作類的文件夾。
如果在當前項目中沒有這個類的命名空間,需要我們手動的導入這個類的所在的命名空間。快捷方式如下:
1)用鼠標去點
2)alt+shift+F10
在一個項目中引用另一個項目的類
1)添加引用。
2)引用命名空間。
值類型和引用類型
區別:1.值類型和引用類型在內存上存儲的地方不一樣
2.在傳遞值類型和傳遞引用類型的時候,傳遞的方式不一樣。值類型為值傳遞,引用類型為引用傳遞。
值類型:int double bool char decimal struct enum
引用類型:string 自定義類 數組
值類型存儲在內存的棧上,引用類型存儲在內存的堆中。

字符串的不可變性
當你給一個字符串重新賦值之后,老值並沒有銷毀, 而是重新開辟一塊空間存儲新值。無論老值還是新值都還保存在堆中
但是,棧中的地址沒刷新成新地址。
當程序結束后,GC掃描整個內存,如果發現有的內存空間沒有被指向,則立即把他銷毀。
我們可以將string看作是char類型的只讀數組。所以我們可以通過下標訪問字符串中額某一個元素。(無法賦值:只讀屬性)
如果想要給字符串的某個元素賦值方法如下:
1.首先將string轉換成char類型的數組。調用方法stringName.ToCharArray();
2.然后獲得相應索引的元素,進行賦值操作。
3.再將字符數組轉換回string。new string(char);
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Panel 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 string s = "abcdefg"; 14 15 char[] chs = s.ToCharArray(); 16 chs[0]= 'b'; 17 s = new string(chs); 18 Console.WriteLine(s); 19 Console.ReadKey(); 20 } 21 } 22 }
字符串提供的各種方法
(1)Length:獲得當前字符串中字符的個數。
(2)ToUpper();將字符串轉換成大寫形式。
(3)ToLower();將字符串轉換成小寫形式。
(4)Equals(stringName,stringComparision.OrdinalIgnoreCase):比較兩個字符串,以各種格式。
(5)Split(char[],StringSplitOptions.RemoveEmptyEnties):分割字符串,返回字符串類型的數組。
(6)Replace(string oldValue,string newValue):將字符串中大的oldValue的地方替換成newValue。
(7)Contains(string value):判斷字符串中是否含有子字符串value。
(8)Substring(int startIndex):取從位置startIndex開始一直到最后的字符串。
(9)Substring(int startIndex,int Length):取從位置startIndex開始長度為length的子字符串,如果
子字符串的長度不足length則報錯。
(10)StartWith(string value):判斷字符串是否以子串value開始。
(11)EndWith(string value):判斷字符串是否以子串value結束。
(12)IndexOf(string value):取子串value第一次出現的位置。找不到返回-1.
(13)Trim():移除所有前導空白字符和尾部空白字符。
(14)string.IsNullOrEmpty():判斷一個字符串是否為空或者null.
(15)string.Join():將數組按照指定的字符串連接,返回一個字符串。
(16)string.Format():格式字符串中各種格式化定義字符。

練習1:接受用戶輸入的字符串,將其中的字符與輸入的順序輸出。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 草稿 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 string str = "abcdefg"; 14 char[] chs = str.ToCharArray(); 15 16 for (int i = 0; i < chs.Length/2; i++) 17 { 18 char temp = chs[i]; 19 chs[i] = chs[chs.Length - 1 - i]; 20 chs[chs.Length - 1 - i] = temp; 21 } 22 str = new string(chs); 23 Console.WriteLine(str); 24 Console.ReadKey(); 25 } 26 } 27 }
練習2:將“hello c sharp”反向輸出
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 草稿 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 string str = "hello c sharp"; 14 string[] strNew = str.Split(new char[] { ' '},StringSplitOptions.RemoveEmptyEntries); 15 for (int i = 0; i < strNew.Length/2; i++) 16 { 17 string temp = strNew[i]; 18 strNew[i] = strNew[strNew.Length-1-i]; 19 strNew[strNew.Length - 1 - i] = temp; 20 } 21 for (int i = 0; i < strNew.Length; i++) 22 { 23 Console.Write(strNew[i]+" "); 24 } 25 Console.ReadKey(); 26 } 27 }
練習3:從email中提取用戶名和域名:abc@163.com(要動態截取)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 草稿 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 string email = "abc@163.com"; 14 int index = email.IndexOf('@'); 15 string userName = email.Substring(0,index); 16 string yuMing = email.Substring(index+1); 17 Console.WriteLine(userName); 18 Console.WriteLine(yuMing); 19 Console.ReadKey(); 20 } 21 } 22 }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
