String类:
.Length  字符的长度

.Trim()  去掉开头以及结尾的空格
.TrimStart() 去掉字符串开头的空格
.TrimEnd() 去掉字符串后面的空格

.ToUpper() 全部大写
.ToLower() 全部小写

Substring(起始位置,截取长度)
Substring(起始位置) 只写起始位置,可以截取到尾
身份证截取生日

IndexOf("字符串") 返回第一次出现此字符串的索引
LastIndexOf("字符串") 返回最后一次出现此字符串的索引

StartsWith("字符串") 是否以此字符串为开头,返回True或False
EndsWith("字符串") 是否以此字符串为结尾
Contains("字符串") 是否包含此字符串。返回True或者False

Replace("老字","新字") 将老字用新字替换

例:

           判断 邮箱格式是否正确
           1. 有且只有一个@
           2.不能以@开头
           3. @后有。
           4.@与。不能在一起
           5.不能以。结尾
            Console.WriteLine("输入一个邮箱");
            string a = Console.ReadLine();
            if (a.Contains("@"))
            {
                int b = a.IndexOf("@");
                int c = a.LastIndexOf("@");
                if (b == c)
                {
                    if (!a.StartsWith("@"))
                    {
                        string d = a.Substring(b);
                        if (d.Contains("."))
                        {
                            if (d.IndexOf(".") != 1 && a.Substring(b - 1, 1) != ".")
                            {
                                if (!a.EndsWith("."))
                                {
                                    Console.WriteLine(a);
                                }
                                else
                                {
                                    Console.WriteLine("格式错误");
                                }
                            }
                            else
                            {
                                Console.WriteLine("格式错误");
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("格式错误");
                    }
                }
                else
                {
                    Console.WriteLine("格式错误");
                }
            }
            else
            {
                Console.WriteLine("格式错误");
            }
            Console.ReadLine();

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

奇数下.5取上线,偶数下.5去下线

随机数类   Random
需要使用随机数的时候需要先初始化
Random ran = new Random();

例:

            四位验证码
            A--Z,a--z,0--9
            不区分大小写
 方法1.  string ss = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            Random ran = new Random();
            int x = ran.Next(62);
            int y = ran.Next(62);
            int z = ran.Next(62);
            int w = ran.Next(62);
            string xx = ss.Substring(x, 1);
            string yy = ss.Substring(y, 1);
            string zz = ss.Substring(z, 1);
            string ww = ss.Substring(w, 1);
            string o = xx + yy + zz + ww;
            Console.WriteLine("验证码"+o);
            Console.WriteLine("对照");
            string shu = Console.ReadLine();
            if (shu.ToUpper() == o.ToUpper())
            {
                Console.WriteLine("正确");
            }
            else
            {
                Console.WriteLine("错误");
            }
            Console.ReadLine();

 方法2.  string ss = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            Random ran = new Random();
            string rect = "";
            for (int i = 0; i < 4; i++)
            {
                int a = ran.Next(62);
                rect += ss.Substring(a, 1);
            }
            Console.WriteLine("验证码是:" + rect);
            Console.Write("请对照输入验证码:");
            string shu = Console.ReadLine();
            if (shu.ToUpper() == rect.ToUpper())
            {
                Console.WriteLine("输入正确!");
            }
            else
            {
                Console.WriteLine("输入错误!");
            }

            Console.ReadLine();

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

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

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

DateTime可以增加或者减去相应的时间
Add()  增加或者减去一定的时间间隔
AddYears() 增加或减去年份
AddMonths() 增加或减去月份
AddDays() 增加或减去天数
以此类推。
注意,加减天数,小时数是利用double类型。其他都是int类型

try   catch
异常保护语句

try尝试

catch若try里面的语句有问题,直接跳到catch执行

  例:

          Console.Write("请输入日期时间:");
            try
            {
                DateTime dt = DateTime.Parse(Console.ReadLine());
                Console.WriteLine("您输入的日期时间格式正确!");
            }
            catch
            {
                Console.WriteLine("您输入的日期时间有误!");
            }
            Console.WriteLine("感谢您的使用!再见!");
            Console.ReadLine();

原文地址:https://www.cnblogs.com/shi2172843/p/5616472.html