Lession10-字符串和正则表达式练习

2021-06-11  本文已影响0人  任人渐疏_Must

String练习

 class Program
    {
        static void Main(string[] args)
        {
            //1练习一:接受用户输入的字符串,将其字符以与输入相反的顺序输出 “abc”->"cba"

            string str = "abcdefg";
            //for (int i=str.Length-1;i>=0;i--)
            //{
            //    Console.Write(str[i]);
            //}
            //Console.WriteLine();
            char[] chs = str.ToCharArray();
            for (int i = 0;i<chs.Length/2;i++)
            {
                /*
                 * chs[1]->chs[chs.length-1-0];
                 * chs[2]->chs[chs.leagth-1-1];
                 * 
                 */
                char temp = chs[i];
                chs[i] = chs[chs.Length - 1 - i];
                chs[chs.Length - 1 - i] = temp;
            }
            str = new string(chs);
            Console.WriteLine(str);

            Console.WriteLine("---------------------------------------------");

            //2练习
            // hello c sharp  ->sharp c hello

            string str2 = "hello c sharp";
            string[] strNew = str2.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < strNew.Length / 2; i++)
            {
                string temp = strNew[i];
                strNew[i] = strNew[strNew.Length - 1 - i];
                strNew[strNew.Length - 1 - i] = temp;
            }
            //foreach(string s in strNew)
            //{
            //    Console.WriteLine(s);
            //}
            //使用Join方法将字符串按照指定的分隔符连接

            str2 = string.Join(" ", strNew);
            Console.WriteLine(str2);
            Console.WriteLine("--------------------------------");
           
            //3练习:从Email中提取用户名和域名:abc@163.com
            string email = "abc@163.com";
            //string username = email.Substring(0, 3);
            //string yuMing = email.Substring(4);
            int index = email.IndexOf("@");
            string username = email.Substring(0, index);
            string yuMing = email.Substring(index+1);

            Console.WriteLine(username);
            Console.WriteLine(yuMing);

            Console.WriteLine("--------------------------------------------");

            //4练习:让用户输入一句话,找出所有e的位置

            string str3 = "adsdhkfjkajjoiewenfjejeen";
            //int index2 = str3.IndexOf('e');
            //Console.WriteLine("第1次出现e的位置是{0}",index2);
            ////循环体:从上一次出现e的位置加1的位置找下一次e出现的位置
            ////循环条件:index2 != -1
            //int count = 1;//用来记录e出现的次数
            //while(index2 != -1)
            //{
            //    count++;
            //    index2 = str3.IndexOf('e', index2 + 1);
            //    if(index2 == -1)
            //    {
            //        break;
            //    }
            //    Console.WriteLine("第{0}次出现e的位置是{1}", count, index2);
            //}

            for(int i = 0; i < str3.Length; i++)
            {
                if (str3[i] == 'e')
                {
                    Console.WriteLine(i);
                }
            }
            Console.WriteLine("---------------------------------------------");

            //5练习:用户输入一句话,判断这句话中有没有牛逼,如果有牛逼的话就替换成“**”
            string str4 = "物联网班很牛逼";
            if (str4.Contains("牛逼"))
            {
                str4 = str4.Replace("牛逼","**");
            }
            Console.WriteLine(str4);

            Console.WriteLine("--------------------------------------");

            //6练习:{"程咬金","猪八戒","孙尚香","鲁班"} 变成 程咬金|猪八戒|孙尚香|鲁班,然后再把|切割掉
            string[] names = { "程咬金", "猪八戒", "孙尚香", "鲁班" };
            string str5 = string.Join("|",names);
            string[] strNew2 = str5.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string name in strNew2)
            {
                Console.WriteLine(name);
            }
            Console.ReadKey();
        }
    }



正则练习

/*
     * 1.判断一个字符串是不是身份证号码
     * 18位,前面17位都是数字,首位不能为0,末尾可能是数字可能是X
     * 
     * ^[1-9][0-9]{16}[0-9xX]$
     * 2. 判断字符串是否是正确的国内电话号码,不考虑分机
     * 010-8888888或010-88888880或010xxxxxxx 0917-8888888或0917-88888888 09178888888(区号-电话号码)
     * 10086 10010 95595 95599(5位)
     * 13888888 888(11位手机号)
     * 
     * a: [0-9]{3,4}-?[0-9]{7,8}
     * b: [0-9]{5}
     * c:[1-9][0-9]{10}
     * ^([0-9]{3,4}-?[0-9]{7,8}|[0-9]{5}|[1-9][0-9]{10})$
     * 
     * 3.判断合法的邮箱
     * xxxx   @  qq/163 .com/.xxxx.cn
     * 用户名:数字、[1-9][0-9]{10}字母、下划线、点、中划线
     * 域名:字母、数字、中划线
     * 后缀:.开头,后面可能字母,后面也有可能是.和字母
     *  ^[0-9a-zA-Z_.\-]+@[a-zA-Z\-0-9]+(\.[a-zA-Z]+){1,2}$
     */
    class Program
    {
        static void Main(string[] args)
        {
            //while (true)
            //{
            //    Console.WriteLine("请输入身份证号:");
            //    string id = Console.ReadLine();
            //    bool b = Regex.IsMatch(id, "^[1-9][0-9]{16}[0-9xX]$");
            //    Console.WriteLine(b);

            //}

            //while (true)
            //{
            //    Console.WriteLine("请输入电话号码:");
            //    string tel = Console.ReadLine();
            //    bool b = Regex.IsMatch(tel, "^([0-9]{3,4}-?[0-9]{7,8}|[0-9]{5}|1[0-9]{10})$");
            //    Console.WriteLine(b);

            //}

            while (true)
            {
                Console.WriteLine("请输入邮箱:");
                string email = Console.ReadLine();
                bool b = Regex.IsMatch(email, "^[0-9a-zA-Z_.\\-]+@[a-zA-Z\\-0-9]+(\\.[a-zA-Z]+){1,2}$");
                Console.WriteLine(b);

            }

            Console.ReadKey();
        }
    }


上一篇下一篇

猜你喜欢

热点阅读