String类:
Lenth 字符的长度 *


Trim() 去前后空格,不会去掉中间的 *
TrimStart() 去掉前空格
TrimEnd() 去掉后空格


ToLower() 转换为小写 *
ToUpper() 转换为大写 *
索引 *
IndexOf("cd") //返回值为2,查找的是第一个匹配项首字母的索引
LastIndexOf("cd") //从最后开始查找,返回第一个匹配项的首字母索引


Contains("aa") //是否包含aa字母 ,返回True或Fals*
StartWith("aa") //是否以aa开头,返回True或Fals
EndWith("aa") //是否以aa结尾,返回True或Fals


Rplace("aa","bb") //将aa替换成bb,如果不重新赋值,那么值不会改变


SubString(3) //去掉012的索引字母,从索引3的字母开始 *
SubString(3,5) //从索引3的字母开始,截取5个字母 *


split() 分割字符串 *

Math类:
Ceiling() 取上线
Floor() 取下线
Math.PI 圆周率
Math.Sqrt() 平方根
Math.Round() 四舍五入(注意奇数偶数下.5不一样的结果)

Round(a,b) a是变量,b是int类型,表示小数点后保留几位,将之后的一位四舍五入掉,可以用循环来四舍五入规整

Pow(6,6)  计算次方,返回的数为6的6次方

Abs() 绝对值

DateTime类:
注意在使用之前需要先初始化一遍。
DateTime dt =new DateTime();
若获取当前时间,可以不用初始化:
DateTime dt =DateTime.Now;//系统当前时间,运行时查询
获取年 dt.Year
获取月 dt.Month
获取日 dt.Day
获取小时 dt.Hour
获取分 dt.Minute
获取秒 dt.Second

Millsecond 毫秒

Dayofyear 一年中第几天

获取这一天是星期几
DayOfWeek d = dt.DayOfWeek;
获取到的是英文。
若想用中文,先d.ToString()
然后根据英文打印出中文。

yyyy 年
MM 月
dd 日
hh 时//12小时制的
mm 分
ss 秒
HH 时//24时制得
以上是代位符。可以在字符串中先占用下这个空位。
string s = dt.ToString("yyyy年MM月dd日hh时mm分ss秒");

DateTime可以增加或者减去相应的时间
Add() 增加或者减去一定的时间间隔
AddYears() 增加或减去年份
AddMonths() 增加或减去月份
AddDays() 增加或减去天数
以此类推。

方法:
AddDays();在日期时间值的基础上加天数,需要一个Double值,返回datetime类型

DateTime dt2 = DateTime.Now //现在的电脑时间
Console.WriteLine(da2); //现在电脑的时间
Console.WriteLine(da2.DayOfWeek); //现在的时间。是星期几
Console.WriteLine(da2.AddDays(30)); // 加30天后是几号
Console.WriteLine(da2.Add(new TimeSpan(30,0,0,0))); //组合加法,加30天0小时0分钟0秒后是什么时间。

注意,加减天数,小时数是利用double类型。其他都是int类型

练习1

判断邮箱格式是否正确
1.有且只能有一个@
2.不能以@开头
3.@之后至少有一个.
4.@和.不能靠在一起
5.不能以.结尾

            //接受用户输入
            Console.Write("请输入邮箱账号:");
            string a = Console.ReadLine();

            //判断是否正确
            if (a.Contains("@"))
            {
                if (a.IndexOf("@") == a.LastIndexOf("@"))
                {
                    if (a.StartsWith("@"))
                    {
                        Console.WriteLine("输入错误");
                    }
                    else
                    {
                        if (a.Substring(a.IndexOf("@")).Contains("."))
                        {
                            if (a.Substring (a.IndexOf("@")).Contains(".")&&!(a.Substring(0,a.IndexOf("@")).Contains(".")))
                            {
                                if (!a.EndsWith("."))
                                {
                                    Console.WriteLine("输入格式正确");
                                }
                                else
                                {
                                    Console.WriteLine("输入错误");
                                }
                            }
                            else
                            {
                                Console.WriteLine("输入错误");
                            }
                        }
                        else
                        {
                            Console.WriteLine("输入错误");
                        }
                    }
                }
                else
                {
                    Console.WriteLine("输入错误");
                }
            }
            else
            {
                Console.WriteLine("输入错误");
            }

            Console.ReadLine();

运算结果

练习2

制作出4位随机数验证码,包含英文大小写及数字
生成验证码:
验证码:xxxx
请输入验证码:xxxx
判断用户输入的是否是4位,不是则提示“验证码位数输入有误”
如果输入时4位,则判断输入是否正确,”输入正确“ || ”输入错误“
当验证码为AbCd时, 输入abcd或ABCD,都正确

            string a = "abcdefghijkmnopqrseuvwxyzAABCDEFGHIJKMNOPQRSTUVWXYZ0123456789";
            string end = "";//获得玩的随机数在这拼接
            Random r = new Random();
            end = a.Substring((r.Next(0, a.Length)), 1);
            end += a.Substring((r.Next(0, a.Length)), 1);
            end += a.Substring((r.Next(0, a.Length)), 1);
            end += a.Substring((r.Next(0, a.Length)), 1);

            Console.WriteLine("验证码是:"+end);

            Console.Write("用户输入验证码:" );
            string yh =Console.ReadLine();
            if (end.Length == 4)
            {
                if (yh.ToUpper() == end.ToUpper())
                {
                    Console.WriteLine("输入正确");
                }
                else
                {
                    Console.WriteLine("输入错误");
                }
            }
            else
            {
                Console.WriteLine("验证码数输入有误");
            }

            Console.ReadLine();

运算结果

posted on 2016-05-26 19:14  斐雪  阅读(138)  评论(0编辑  收藏  举报