string实例练习

  1. 输入字符串,将其反转输出”abc”-->”cba”

       static void Main(string[] args)

        {

            Console.WriteLine("请输入一个字符串");

            string str = Console.ReadLine();

            str = Reverse(str);

            Console.WriteLine(str);

 

            Console.Read();

        }

 

        public static string Reverse(string input)

        {

            char[] chs = input.ToCharArray();

            for(int i=0;i

            {

                char ch;

                ch = chs[i];

                chs[i] = chs[chs.Length - i - 1];

                chs[chs.Length - i - 1] = ch;

            }

 

            return new string(chs);

        }

 

  1. 输入字符串,将其按以下进行反转I Love you-->I evoL uoy

        static void Main(string[] args)

        {

            Console.WriteLine("请输入一句话:");

            string[] strs = Console.ReadLine().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            for(int i =0;i

            {

                strs[i] = Reverse(strs[i]);

            }

            Console.WriteLine(string.Join(" ", strs));

            Console.Read();

        }

 

        public static string Reverse(string input)

        {

            char[] chs = input.ToCharArray();

            for(int i=0;i

            {

                char ch;

                ch = chs[i];

                chs[i] = chs[chs.Length - i - 1];

                chs[chs.Length - i - 1] = ch;

            }

 

            return new string(chs);

        }

  1. 2020年7月16日的年月日取出来,打印出来

方法1:

        static void Main(string[] args)

        {

            string str = DateTime.Now.ToLongDateString();

 //         string[] strs = str.Split('年', '月', '日');

string[] strs = str.Split("年月日".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);

            for(int i =0;i

            {

                Console.Write(strs[i]+"  ");

            }

            Console.Read();

        }

方法2:

        static void Main(string[] args)

        {

            string str = DateTime.Now.ToLongDateString();

            int iYear, iMonth, iDay;

            iYear = str.IndexOf('年');

            iMonth = str.IndexOf('月');

            iDay = str.IndexOf('日');

            string str1 = str.Substring(0, iYear);

            string str2 = str.Substring(iYear+1, iMonth - iYear-1);

            string str3 = str.Substring(iMonth+1, iDay - iMonth-1);

            Console.WriteLine("{0}   {1}   {2}", str1, str2, str3);

            Console.Read();

        }

你可能感兴趣的:(string实例练习)