using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 取年月日的字符串方法 { class Program { static void Main(string[] args) { Question(); } public static void Exercise1() { // 輸入一個字符串,然後反轉 Console.WriteLine("請輸入字符串"); string input = Console.ReadLine(); #region MyRegion //char[] chs = input.ToCharArray(); //for (int i = 0; i < chs.Length / 2; i++) //{ // char cTemp = chs[i]; // chs[i] = chs[chs.Length - 1 - i]; // chs[chs.Length - 1 - i] = cTemp; //} //Console.WriteLine(new string(chs)); #endregion // ctrl + k + s + 選擇 Console.WriteLine(MyReverseString(input)); Console.ReadKey(); } /// <summary> /// 反轉字符串 /// </summary> /// <param name="input">輸入的字符串</param> /// <returns>返回翻轉後的字符串</returns> private static string MyReverseString(string input) { char[] chs = input.ToCharArray(); for (int i = 0; i < chs.Length / 2; i++) { char cTemp = chs[i]; chs[i] = chs[chs.Length - 1 - i]; chs[chs.Length - 1 - i] = cTemp; } return new string(chs); } public static void Exercise2() { Console.WriteLine("請輸入一句話"); string input = Console.ReadLine(); string[] wds = input.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < wds.Length; i++) { wds[i] = MyReverseString(wds[i]); } string res = string.Join(" ", wds); Console.WriteLine(res); Console.ReadKey(); } public static void Exercise3() { // Console.WriteLine("不要再方法中寫這個"); // 7 890 string str = "今天是201415年17月324日 10:23:56"; // string[] res = str.Split("年月日".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); int yearIndex = str.IndexOf('年'); int yearStartIndex = GetIndex(str, yearIndex);// 從年開始往前找,第一個不是數字的下標 int monthIndex = str.IndexOf('月'); int dayIndex = str.IndexOf('日'); string year = str.Substring(yearStartIndex + 1, yearIndex - yearStartIndex - 1);//+1防止-1下標出現,Substring第一個參數,下標非數字 年下標右移一位,第二個參數:長度:年下標-月下標-1 string month = str.Substring(yearIndex + 1, monthIndex - yearIndex - 1); string day = str.Substring(monthIndex + 1, dayIndex - monthIndex - 1); Console.WriteLine(year); Console.WriteLine(month); Console.WriteLine(day); Console.ReadKey(); } private static int GetIndex(string str, int index) { for (int i = index - 1; i >=0 ; i--)//從右往左找,從year開始往前找 { if(!char.IsDigit(str[i]))//IsDigit是數字 { return i; } } // 表示開始就是數字 return -1; } public static void Exercise4() { string[] lines = File.ReadAllLines("phone.csv", Encoding.Default); for (int i = 0; i < lines.Length; i++) { string[] ts = lines[i].Split(','); Console.WriteLine("姓名:{0},電話:{1}", ts); } Console.ReadKey(); } public static void Question() { // StringBuilder AppendFormat方法 //StringBuilder sb = new StringBuilder(); //sb.AppendFormat("{0}{1}{2}", "測試", 123, true); //sb.Append(string.Format("{0}{1}{2}", "測試", 123, true)); // IndeOfAny string s = "1999年1月2日"; Dictionary<char, int> dic = new Dictionary<char, int>(); //Dictionary<char, List<int>> dic1 = new Dictionary<char, List<int>>(); //List<Dictionary<char, int>> list = new List<Dictionary<char, int>>(); int index = -1; do { index = s.IndexOfAny("年月日".ToCharArray(), index + 1); if (index != -1) { // Console.WriteLine(index); dic.Add(s[index], index); } } while (index != -1); foreach (KeyValuePair<char, int> item in dic) { Console.WriteLine(item.Key + ", " + item.Value); } Console.ReadKey(); } } }