string类
//string a = "Hello word"; //Console.WriteLine(a.Length);//字符的长度 //Console.WriteLine(a.Trim());//去掉开头以及结尾的空格 //Console.WriteLine(a.TrimStart());//去掉字符串开头的空格 //Console.WriteLine(a.TrimEnd());//去掉字符串后面的空格 //Console.WriteLine(a.ToUpper());//全部大写 //Console.WriteLine(a.ToLower());//全部小写 //Console.WriteLine(a.Substring(0, 2));//截取起始位置,截取长度 //Console.WriteLine(a.Substring(0));//只写起始位置,可以截取到尾 //Console.Write(a.TrimStart());//去掉前面的空格 //Console.Write(a.TrimEnd());//去掉结尾处的空格 //Console.WriteLine(a.IndexOf("rr")); //查找第一次出现该字符或者字符串的索引,返回值为-1表示找不到该字符或者字符串 //Console.WriteLine(a.LastIndexOf("o")); //查找最后一次出现该字符或者字符串的索引返回值为-1表示找不到该字符或者字符串 //Console.WriteLine(a.StartsWith("a"));//是否以此字符串为开头,返回True或False //Console.WriteLine(a.EndsWith("o"));//是否以此字符串为结尾 返回True或者False //Console.WriteLine(a.Contains("e"));//是否包含此字符串。返回True或者False //Console.WriteLine(a.Replace("o","1"));//Replace("老字","新字") 将老字用新字替换
判断邮箱格式是否正确:
1.有且只能有一个@ 2.不能以@开头 3.@之后至少有一个. 4.@和.不能靠在一起 5.不能以.结尾
Console.WriteLine("请输入你的邮箱格式"); string mail = Console.ReadLine(); if (mail.Contains("@"))//判断有没有@ { int b = mail.IndexOf("@");//查找第一次出现该字符的索引 int c = mail.LastIndexOf("@");//查找最后一次出现该字符的索引 if (b == c)//判断是否只有一个@ { if (!mail.StartsWith("@"))//判断不是以@开头 { string d = mail.Substring(b);//截取索引 if (d.Contains("."))//判断有没有. { string aa = mail.Substring(b - 1, 1);// 截取@前面的 string bb = mail.Substring(1, 1);//截取@后面的 if (aa != "." && bb != ".")//判断前后都没有. { if (!mail.EndsWith("."))//判断是不是以.结尾 { Console.WriteLine("格式正确"); } else { Console.WriteLine("格式错误"); } } else { Console.WriteLine("格式错误"); } } else { Console.WriteLine("格式错误"); } } else { Console.WriteLine("格式错误"); } } else { Console.WriteLine("格式错误"); } } else { Console.WriteLine("格式错误"); }
3.输入身份证号 判断出生年月日
Console.WriteLine("请输入身份证号"); string a = Console.ReadLine(); string b = a.Substring(6, 4); string c = a.Substring(10, 2); string d = a.Substring(12, 2); Console.WriteLine("你的出生日期为,{0}年{1}月{2}日", b, c, d);
math 类:
//取上限 天花板 /* double a = 4.4; Console.WriteLine(Math.Ceiling(a)); //取下限 地板 Console.WriteLine(Math.Floor(a)); //圆周率 π Console.WriteLine(Math.PI); //平方根 Console.WriteLine(Math.Sqrt(4)); //四舍五入 分奇数.5和偶数.5之分 //奇数.5情况取上限 //偶数.5情况取下限 Console.WriteLine(Math.Round(1.5)); //取绝对值 Console.WriteLine(Math.Abs(-10)); //指定数值的指定次幂 Console.WriteLine(Math.Pow(2,2));*/
获取月 dt.Month
获取日 dt.Day
获取年 dt.Year
获取小时 dt.Hour
获取分 dt.Minute
获取秒 dt.Second
计算北京奥运会已经开幕多久了?
//计算一下北京奥运会已经开了多少天 //DateTime dt1 = DateTime.Parse("2008/8/8 20:00:00"); //DateTime dt2 = DateTime.Now; //Console.WriteLine(Math.Floor((dt2 - dt1).TotalDays));
验证码
//string yzm = "QAZWSXEDCRFVTGBYHNUJMIKOLPqazwsxedcrfvtgbyhnujmikolp123456789"; //Random dt = new Random();//生成随机数 //int a = dt.Next(1, 61);//定义范围 //int b = dt.Next(1, 61); //int c = dt.Next(1, 61); //int d = dt.Next(1, 61); //string aa = yzm.Substring(a, 1);//Substring(起始位置,截取长度) //string bb = yzm.Substring(b, 1);//Substring(起始位置,截取长度) //string cc = yzm.Substring(c, 1);//Substring(起始位置,截取长度) //string dd = yzm.Substring(d, 1);//Substring(起始位置,截取长度) //string z = aa + bb + cc + dd; //Console.WriteLine(z); //Console.WriteLine("请输入验证码"); //string y = Console.ReadLine(); //z = y.ToLower();//转换成小写 //y = y.ToLower();//转换成大写 //if (z == y) //{ // Console.WriteLine("正确"); //} //else //{ // Console.WriteLine("验证码错误"); //}
石头剪刀布
//Console.WriteLine("1剪刀 2石头 3布"); //Random df = new Random(); //int s = df.Next(1,4); //Console.WriteLine("请出拳"); //int ii = int.Parse(Console.ReadLine()); //int d = ii - s; //if(d==1||d==-2) //{ // Console.WriteLine("人赢"); //} //if(d==2||d==-1) //{ // Console.WriteLine("人负"); //} //if(d==0) //{ // Console.WriteLine("平局"); //}
输入金额转换成大写
//壹贰叁肆伍陆柒捌玖零 //Console.WriteLine("请输入金额"); //string a = Console.ReadLine(); //string b = "零壹贰叁肆伍陆柒捌玖"; //int c = a.Length;//判断长短 //if (c == 4) //{ // int a1 = int.Parse(a.Substring(0, 1));//截取千位数字 // int a2 = int.Parse(a.Substring(1, 1)); // int a3 = int.Parse(a.Substring(2, 1)); // int a4 = int.Parse(a.Substring(3, 1)); // string b1 = b.Substring(a1, 1);//将千位数字转换为大写数字 // string b2 = b.Substring(a2, 1); // string b3 = b.Substring(a3, 1); // string b4 = b.Substring(a4, 1); // Console.WriteLine(b1 + "仟" + b2 + "佰" + b3 + "拾" + b4); //} //if (c == 3) //{ // int a1 = int.Parse(a.Substring(0, 1)); // int a2 = int.Parse(a.Substring(1, 1)); // int a3 = int.Parse(a.Substring(2, 1)); // string b1 = b.Substring(a1, 1); // string b2 = b.Substring(a2, 1); // string b3 = b.Substring(a3, 1); // Console.WriteLine(b1 + "佰" + b2 + "拾" + b3); //} //if (c == 2) //{ // int a1 = int.Parse(a.Substring(0, 1)); // int a2 = int.Parse(a.Substring(1, 1)); // string b1 = b.Substring(a1, 1); // string b2 = b.Substring(a2, 1); // Console.WriteLine(b1 + "拾" + b2); //} //if (c == 1) //{ // int a1 = int.Parse(a.Substring(0, 1)); // string b1 = b.Substring(a1, 1); // Console.WriteLine(b1); //}
异常保护 try{}catch(){}finally{}
Console.Write("请输入一个整数:"); try //尝试去做,如果有错,从错误的句直接跳转到catch { int zhi = Convert.ToInt32(Console.ReadLine()); } catch(Exception ex) //只有错误的情况下 { Console.WriteLine(ex.ToString()); } finally //不论对错都会执行 { } 示例