取年月日的字符串方法

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();
        }
    }
}
原文地址:https://www.cnblogs.com/blacop/p/6006565.html