复习类

string 类:

string a = " asdfgHVj88";
int b = a.Length; //长度 属性不用加();方法要加()
Console.WriteLine(b);
string c = a.Trim(); //去掉前后空格
Console.WriteLine(c);
string d = a.TrimStart(); //去掉前空格
Console.Write("\n"); //空格
Console.WriteLine(d);
string e = a.TrimEnd(); //去掉后空格
string f = a.ToUpper(); //全部将字母小写换为大写(验证码转换)
string g = a.ToLower(); //全部将字母大写换为小写

//索引号是从0开始的
string h = a.Substring(4); //里面那一个值,表示从这个开始索引到最后
Console.WriteLine(h);
Console.WriteLine(a.Substring(8));//不重新赋值,a是没有变化的
string i = a.Substring(4, 3); //两个值表示从哪个索引号开始截取多少长度
Console.WriteLine(i);


string j = a.Replace("df", "DF"); //替换大小写
Console.WriteLine(j);

string k = "2012/12/23";
string[] aa = k.Split(); //分割字符串,以什么字符。

bool y = a.Contains("ff"); //判断是否包含此字符
bool n = a.StartsWith("a"); //是否以此字符串为开头,返回True或False
bool m = a.EndsWith("a"); //是否以此字符串为结尾,返回True或False
int z = a.IndexOf("d"); //从前面开始找到第一个d,从前往后数他的索引号
int w = a.LastIndexOf("d"); //从后面开始找到第一个d,从前往后数他的索引号

 

 

案例:

  // 1. 输入身份证号,截取生日,输出

 Console.Write("请输入身份证号码:");
            string a= Console.ReadLine();
            int b = a.Length;
            if (b == 18)
            {
                string n = a.Substring(6, 4);
                string y = a.Substring(10, 2);
                string r = a.Substring(12, 2);
                Console.WriteLine("您的生日是:"+n+""+y+""+r+"日。");
            }
            else
            {
                Console.WriteLine("您的输入有误!");
            }
            Console.ReadLine();

 

2   练习:判断邮箱格式是否正确

//1.有且只能有一个@ 
//2.不能以@开头 
//3.@之后至少有一个. 
//4.@和.不能靠在一起 
//5.不能以.结尾

 

   Console.Write("请输入邮箱帐号:");
            string mail = Console.ReadLine();
            bool a = mail.Contains("@");
            if (a == true)
            {
                int b=mail.IndexOf("@");
                int c=mail.LastIndexOf("@");
                if (b == c)//1.有且只能有一个@  
                {
                    if (b != 0) //2.不能以@开头    
                    { 
                       string  mail1=mail.Substring(b);
                       bool d = mail1.Contains(".");//)3.@之后至少有一个
                       if (d == true)
                       {
                           int e = mail1.IndexOf(".");
                           if (e != 1)  //4.@和.不能靠在一起   
                           {
                               int f = mail1.LastIndexOf(".");
                               if (f != mail1.Length - 1)  //5.不能以.结尾       
                               {
                                   Console.WriteLine("您输入的邮箱格式正确!");
                               }
                               else
                               {
                                   Console.WriteLine("您的输入有误!");
                               }
                           }
                           else
                           {
                               Console.WriteLine("您的输入有误!");
                           }
                       }
                       else
                       {
                           Console.WriteLine("您的输入有误!");
                       }
                    }

                    
                    else
                    {
                        Console.WriteLine("您的输入有误!");
                    }
                }
                else
                {
                    Console.WriteLine("您的输入有误!");
                }
            }
            else
            {
                Console.WriteLine("您的输入有误!");
            }
            Console.ReadLine();

 

随机数类:Random
//Random ran = new Random();   //初始化
// double a= ran.Next(10);
// Console.WriteLine(a);

案例: //随机出验证码,对照输入,判断是否正确

 string a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            Random ran = new Random();
            for (; ; )
            {
                string biao = "";
                for (int i = 0; i < 4; i++)
                {
                    biao += a.Substring(ran.Next(a.Length), 1);                    
                }
                Console.WriteLine(biao);
                Console.Write("请输入验证码:");
                string shu = Console.ReadLine();
                if (biao.ToLower() == shu.ToLower())
                {
                    Console.WriteLine("您的输入正确!");
                    break;

                }
                else
                {
                    Console.Clear();
                    Console.WriteLine("您的输入有误,请重新输入!");                   
                }
            }
            Console.ReadLine();

 

posted @ 2016-03-12 16:45  陌上初薰  阅读(163)  评论(0编辑  收藏  举报