1. 程式人生 > 實用技巧 >字串的各種方法.

字串的各種方法.

  1     class Program
  2     {
  3         static void Main(string[] args)
  4         {
  5             //Console.WriteLine("請輸入你喜歡的課程");
  6             //string lessonOne = Console.ReadLine();
  7             ////將字串轉換成大寫
  8             ////lessonOne = lessonOne.ToUpper();
  9             ////將字串轉換成小寫形式
 10             ///
/lessonOne = lessonOne.ToLower(); 11 //Console.WriteLine("請輸入你喜歡的課程"); 12 //string lessonTwo = Console.ReadLine(); 13 ////lessonTwo = lessonTwo.ToUpper(); 14 ////lessonTwo = lessonTwo.ToLower(); 15 //if (lessonOne.Equals(lessonTwo,StringComparison.OrdinalIgnoreCase))
16 //{ 17 // Console.WriteLine("你們倆喜歡的課程相同"); 18 //} 19 //else 20 //{ 21 // Console.WriteLine("你們倆喜歡的課程不同"); 22 //} 23 //Console.ReadKey(); 24 25 /*分割字串Split*/ 26 //string s = "a b dfd _ + = ,,, fdf ";
27 //char[] chs = { ' ', '_', '+', '=', ',' }; 28 //string[] str = s.Split(chs, StringSplitOptions.RemoveEmptyEntries); 29 //for (int i=0;i<str.Length;i++) 30 //{ 31 // Console.WriteLine(str[i]); 32 //} 33 //Console.ReadKey(); 34 35 36 //練習:從日期字串("2008-08-08")中分析出年、月、日;2008年08月08日。 37 //讓使用者輸入一個日期格式如:2008-01-02,你輸出你輸入的日期為2008年1月2日 38 //string s = "2008-08-08"; 39 //char[] chs = { '-' }; 40 //string[] date = s.Split(chs, StringSplitOptions.RemoveEmptyEntries); 41 //Console.WriteLine("{0}年{1}月{2}日", date[0], date[1], date[2]); 42 //Console.ReadKey(); 43 44 45 /*老趙*/ 46 //string str = "國家關鍵人物老趙"; 47 //if (str.Contains("老趙")) 48 //{ 49 // str = str.Replace("老趙", "**"); 50 //} 51 //Console.WriteLine(str); 52 //Console.ReadKey(); 53 54 55 /*Substring 擷取字串*/ 56 //string str = "今天天氣好晴朗,處處好風光"; 57 //str = str.Substring(1,2); 58 //Console.WriteLine(str); 59 //Console.ReadKey(); 60 61 62 //string str = "今天天氣好晴朗,處處好風光"; 63 //if (str.EndsWith("風")) 64 //{ 65 // Console.WriteLine("是的"); 66 //} 67 //else 68 //{ 69 // Console.WriteLine("不是的"); 70 //} 71 //Console.ReadKey(); 72 73 74 //string str = "今天天氣好晴朗,天天處天好風光"; 75 //int index = str.IndexOf('哈', 2); 76 //Console.WriteLine(index); 77 //Console.ReadKey(); 78 79 80 //string str = "今天天氣好晴朗,處處好風光"; 81 //int index = str.LastIndexOf('天'); 82 //Console.WriteLine(index); 83 //Console.ReadKey(); 84 85 86 /*LastIndexOf Substring*/ 87 //string path = @"c:\a\b\c蒼\d\e蒼\f\g\\fd\fd\fdf\d\vfd\蒼老師蒼.wav"; 88 //int index = path.LastIndexOf("\\"); 89 //path = path.Substring(index + 1); 90 //Console.WriteLine(path); 91 //Console.ReadKey(); 92 93 /*去掉空格*/ 94 //string str = " hahahah "; 95 //str = str.Trim(); 96 //str = str.TrimStart(); 97 //str = str.TrimEnd(); 98 //Console.Write(str); 99 //Console.ReadKey(); 100 101 /*判斷是否為空*/ 102 //string str = "fdsfdsfds"; 103 //if (string.IsNullOrEmpty(str)) 104 //{ 105 // Console.WriteLine("是的"); 106 //} 107 //else 108 //{ 109 // Console.WriteLine("不是"); 110 //} 111 //Console.ReadKey(); 112 113 /*張三|李四|王五|趙六|田七*/ 114 //string[] names = { "張三", "李四", "王五", "趙六", "田七" }; 115 //string strNew = string.Join("|", "張三","李四","王五","趙六","田七"); 116 //Console.WriteLine(strNew); 117 //Console.ReadKey(); 118 119 } 120 }