1.regex正則表達式
Regex re = new Regex("abc");編程
個人理解是:對「abc」這個字符進行代換或者判斷...,即將「abc」當作關注點。app
關於正則表達式,有一些code較爲經常使用。異步
#1忽略大小寫的功能RegexOptions.IgnoreCaseasync
3 string str1 = "hello"; 4 string str2 = "HeLLo"; 5 Regex re = new Regex("hello", RegexOptions.IgnoreCase); 6 if(re.IsMatch(str1)) 7 { 8 Console.WriteLine("it is match!"); 9 } 10 if (re.IsMatch(str2)) 11 { 12 Console.WriteLine("it is match,too!"); 13 }
輸出爲:異步編程
#2 替換功能Regex.Replace()函數
1 string str1 = "123abc321AbC123"; 2 Console.WriteLine("before replace:{0}", str1); 3 Regex re = new Regex("abc", RegexOptions.IgnoreCase); 4 string newStr = re.Replace(str1, "|"); 5 Console.WriteLine("after replace:{0}",newStr);
輸出爲:測試
#3.匹配IP地址spa
1 string str1 = "04:09:54 111.13.100.91 www.baidu.com" 2 + "05:22:23 112.25.24.135 www.sohu.com " 3 + "08:09:11 23.200.221.15 www.apple.com"; 4 Regex re = new Regex(@"(?<time>(\d|\:)+)\s" + 5 @"(?<ip>(\d|\.)+)\s" + 6 @"(?<site>\S+)"); 7 MatchCollection matches = re.Matches(str1); 8 foreach(Match match in matches) 9 { 10 if(match.Length != 0) 11 { 12 Console.WriteLine("\nmatches: {0}", match.ToString()); 13 Console.WriteLine("time: {0}", match.Groups["time"]); 14 Console.WriteLine("ip: {0}", match.Groups["ip"]); 15 Console.WriteLine("site: {0}", match.Groups["site"]); 16 } 17 }
結果:code
2.async & await
這是兩個關鍵字,用於異步編程。像寫同步方法同樣去寫異步方法。async關鍵字能用在方法、lambda表達式的聲明部分,用來標示此方法可能包含await關鍵字,只有擁有async才能在其內部使用await關鍵字。當一個async方法,且內部包含await關鍵字,它就會在編譯的時候成爲一個異步方法,若是沒有await關鍵字,則它將只會被當成一個同步方法來執行。
下面是從網上查找到,普通同步與用了async & await的異步的比較程序。(本身加了程序運行時間)
先是同步:
1 static void Main(string[] args) 2 { 3 DateTime d1 = DateTime.Now; 4 // 同步方式 5 Console.WriteLine("同步方式測試開始!"); 6 SyncMethod(0); 7 Console.WriteLine("同步方式結束!"); 8 9 DateTime d2 = DateTime.Now; 10 Console.WriteLine("花費時間爲:{0}",d2-d1); 11 12 } 13 // 同步操做 14 private static void SyncMethod(int input) 15 { 16 Console.WriteLine("進入同步操做!"); 17 var result = SyancWork(input); 18 Console.WriteLine("最終結果{0}", result); 19 Console.WriteLine("退出同步操做!"); 20 } 21 // 模擬耗時操做(同步方法) 22 private static int SyancWork(int val) 23 { 24 for (int i = 0; i < 5; ++i) 25 { 26 Console.WriteLine("耗時操做{0}", i); 27 Thread.Sleep(100); 28 val++; 29 } 30 return val; 31 }
結果:
而後是異步:
1 static void Main(string[] args) 2 { 3 // 異步方式 4 DateTime d1 = DateTime.Now; 5 Console.WriteLine("\n異步方式測試開始!"); 6 AsyncMethod(0); 7 Console.WriteLine("異步方式結束!"); 8 Console.ReadKey(); 9 DateTime d2 = DateTime.Now; 10 Console.WriteLine("程序運行時間:{0}",d2-d1); 11 } 12 13 // 異步操做 14 private static async void AsyncMethod(int input) 15 { 16 Console.WriteLine("進入異步操做!"); 17 var result = await AsyncWork(input); 18 Console.WriteLine("最終結果{0}", result); 19 Console.WriteLine("退出異步操做!"); 20 } 21 22 // 模擬耗時操做(異步方法) 23 private static async Task<int> AsyncWork(int val) 24 { 25 for (int i = 0; i < 5; ++i) 26 { 27 Console.WriteLine("耗時操做{0}", i); 28 await Task.Delay(100); 29 val++; 30 } 31 return val; 32 }
結果爲:
由結果可知,耗時操做此事已變成異步進行了,即並不會等待下面的AsyncWork完成其耗時操做,先返回main函數,而後再進行耗時操做。通過比較可知,兩者程序結構同樣,只是異步的多了兩個關鍵字async & await,就能夠用寫同步的方法來寫異步程序了。
over