Unity3D开发-C#语言进阶篇(字符串String应用)

 class Program
    {
        static void Main(string[] args)
        {
            //  给定一个字符串例如:a = “aCReFcbdeFewesf”;
            //问题如下: 
            //1、 把字符串a转化为小写保存到b字符串中
            //2、 把a字符串中F之间的字符取出来保存为c字符串,然后把c转化存到数组Arr中; 
            //3、 把b字符串和c字符串连接起来保存为d字符串
            //4、 再把d字符串的内容存到数组Arr中;
            //5、 找出a字符串中出现次数最多的字符和出现的次数
            //11、bb = "2008-08-08" 把日期b字符串转化为字符串 b = 2008年8月8日;
            //12、cc =“2012年12月21日”从日期字符串中把年月日分别取出来,打印到控制台;
            //拓展题:ff =“abcdefghigklmnopqrstuvwx”,要求显示出一个6行4列的二维数组矩阵;

            Console.WriteLine("--------------第一题---------------");
            string a = "aCReFcbdeFewesf";
            string b = a.ToLower();
            Console.WriteLine(b);//第一题

            int index1 = a.IndexOf("F");
            int index2 = a.LastIndexOf("F");


            Console.WriteLine("\n--------------第二题--------------");
            string c = a.Substring(index1 + 1, index2 - index1 - 1);
            char[] Arr = new char[c.Length];
            Arr = c.ToArray();
            Console.WriteLine(Arr);//第二题
            foreach (char i in Arr)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine("\n--------------第三题--------------");
            string d = String.Concat(b, c);
            Console.WriteLine("\n" + d);//第三题



            Console.WriteLine("\n--------------第四题--------------\n");
            d = d.Insert(0, c);
            //Console.WriteLine(d);
            Arr = new char[d.Length];
            Arr = d.ToArray();
            foreach (char i in Arr)
            {
                Console.Write(i + " ");
            }
            //Console.WriteLine(Arr);//第四题

            Console.WriteLine("\n--------------第五题--------------\n");
            //            给定一个字符串例如:a = “aCReFcbdeFewesf”;
            a = "aCReFcbdeFewesf";
            int[] aa = new int[a.Length];
            int count = 0;
            for (int i = 0; i < a.Length; i++)
            {
                for (int j = i; j < a.Length; j++)
                {
                    if (a[i] == a[j])
                    {
                        count++;
                    }
                }
                aa[i] = count;
                count = 0;
            }
            int Max = aa[0];

            for (int i = 0; i < aa.Length; i++)
            {
                if (Max < aa[i])
                {
                    Max = aa[i];
                }
            }
            int indexa1 = Array.IndexOf(aa, Max);
            //Console.WriteLine(indexa1);
            char strMax = a[indexa1];
            Console.WriteLine("出现次数最多的是:{0}  次数为:{1}", a[indexa1], Max);





            Console.WriteLine("\n--------------第11题--------------\n");
            string bb = "2008-08-08";
            int indexb1 = bb.IndexOf("-");
            bb = bb.Remove(indexb1, 2);
            //Console.WriteLine(bb);
            int indexb2 = bb.IndexOf("-");
            bb = bb.Remove(indexb2, 2);
            //Console.WriteLine(bb);
            bb = bb.Insert(4, "年");
            //Console.WriteLine(bb);
            bb = bb.Insert(6, "月");
            bb = bb.Insert(bb.Length, "日");
            Console.WriteLine(bb);

            Console.WriteLine("\n--------------第12题--------------\n");
            string cc = "2012年12月21日";
            int indexC1 = cc.IndexOf("年");
            int indexC2 = cc.IndexOf("月");
            int indexC3 = cc.IndexOf("日");

            string year = cc.Substring(0, indexC1 + 1);
            string month = cc.Substring(indexC1 + 1, indexC2 - indexC1);
            string day = cc.Substring(indexC2 + 1, indexC3 - indexC2);
            Console.WriteLine("{0}\n{1}\n{2}", year, month, day);

            Console.WriteLine("\n--------------拓展题--------------\n");
            //拓展题:ff =“abcdefghigklmnopqrstuvwx”
            string ff = "abcdefghigklmnopqrstuvwx";
            char[] arrstr1 = new char[ff.Length];
            arrstr1 = ff.ToCharArray();
            char[,] arrStr = new char[6, 4];
            int k = 0;
            for (int i = 0; i < arrStr.GetLength(0); i++)
            {
                //arrStr[i] = new string[4];
                for (int j = 0; j < arrStr.GetLength(1); j++)
                {
                    arrStr[i, j] = arrstr1[k];
                    //arrStr[i, j] = arrstr1[i * arrStr.GetLength(1)+j];//第二种赋值方法;
                    Console.Write(arrStr[i, j] + " ");
                    k++;
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }

你可能感兴趣的:(Unity3D开发-C#语言进阶篇(字符串String应用))