• String类

                字符串当中索引号是从0开始

                //长度length

                string a = "   Hello   ";

                int b = a.Length;

                Console.WriteLine(b);//输出=11

                Console.Write(a);

                //去掉字符串前后空格

                 Console.Write(a.Trim());

                //去掉前面的空格

                Console.Write(a.TrimStart());

                //去掉后面的空格

                Console.Write(a.TrimEnd());

                //将所有写转换小写

                Console.WriteLine(a.ToLower());

                //将所有小写字母转换为大写

                Console.WriteLine(a.ToUpper());

                //截取字符串,从开始索引截取到最后

                Console.WriteLine(a.Substring(5));

                //截取字符串,从开始索引截取指定的长度

                Console.WriteLine(a.Substring(5, 2));//2指从第五个开始取两个字符

                //替换

                 Console.WriteLine(a.Replace("l", "a"));

                //查找开头第一次出现的索引号,返回值为-1.表示没有找到

                Console.WriteLine(a.IndexOf("l"));

                //从后向前找,从开头开始读

                Console.WriteLine(a.LastIndexOf("l"));

                //查看是否空格开头

                Console.WriteLine(a.StartsWith(" "));

                //查看是否o结尾

                Console.WriteLine(a.EndsWith("o"));

                //查看是否包含a

                Console.WriteLine(a.Contains("a"));

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

                Console.Write("请输入您的邮箱账号:");
                string mail = Console.ReadLine();
                if (mail.Contains("@"))
                {
                    int a = mail.IndexOf("@");
                    int b = mail.LastIndexOf("@");
                    if (a == b)
                    {
                        if (!mail.StartsWith("@"))
                        {
                            string mail1 = mail.Substring(a);
                            if (mail1.Contains("."))
                            {
                                if (mail1.IndexOf(".") != 1 && mail.Substring(a - 1, 1) != ".")
                                {
                                    if (!mail.EndsWith("."))
                                    {
                                        Console.WriteLine("输入的邮箱格式正确!您输入的账号是:" + mail);
                                    }
                                    else
                                    {
                                        Console.WriteLine("格式错误!");
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("格式错误!");
                                }
                            }
                            else
                            {
                                Console.WriteLine("格式错误!");
                            }
                        }
                        else
                        {
                            Console.WriteLine("格式错误!");
                        }
                    }
                    else
                    {
                        Console.WriteLine("格式错误!");
                    }
                }
                else
                {
                    Console.WriteLine("格式错误!");
                }
                Console.ReadLine();

    数学类    Math

                 //取上线

                double a = 3.14;

                Console.WriteLine(Math.Ceiling(a));

                //取下线

                Console.WriteLine(Math.Floor(a));

                //开平方根

                Console.WriteLine(Math.Sqrt(a));

                //圆周率

                Console.WriteLine(Math.PI);

                //四舍五入

                Console.WriteLine(Math.Round(1.5));//奇数。5的情况下取上线   

                Console.WriteLine(Math.Round(2.5));//偶数。5的情况下取下线

    随机数类   Random
               

                需要使用随机数需要先初始化
                Random ran = new Random();
                int a = ran.Next(10);
                Console.WriteLine(a);

                

    例:验证码:随机出四位验证码 (1)A~Z;a~z;0~9  (2)不区分大小写
               

                string ss = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghjklmnopqrstuvwxyz0123456789";
                Random ran = new Random();//随机数类的初始化
                //int a = ran.Next(62);
                //int b = ran.Next(62);
                //int c = ran.Next(62);
                //int d = ran.Next(62);
                //string aa = ss.Substring(a, 1);
                //string bb = ss.Substring(b, 1);
                //string cc = ss.Substring(c, 1);
                //string dd = ss.Substring(d, 1);
                //string rect = aa + bb + cc + dd;
                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();

                Console.Write(" 请输入一个日期时间:****/**/** **:**:**");

                dt = DateTime.Parse( Console.ReadLine());

                Console.WriteLine(dt);

                //若直接获取当前时间不用进行初始化

                DateTime dt1 = DateTime.Now;

                Console.WriteLine(dt1);

                //在dt1身上增加10

                Console.WriteLine(dt1.AddDays(10));

                //增加10个小时

                Console.WriteLine(dt1.AddHours(10));

                //创建时间间隔

                TimeSpan time = new TimeSpan(10,10,10,10);

                Console.WriteLine(dt1.Add(time));

              

                获取年  dt.Year

                获取月  dt.Month

                获取日  dt.Day

                获取小时  dt.Hour

                获取分  dt.Minute

                获取秒  dt.Second

                Console.WriteLine(dt1.Hour);

                //获取这一天星期几

                DayOfWeek dw = dt1.DayOfWeek;

                 //将获取到的星期转换成中文

                switch (dw.ToString())

                {

                    case "Monday":

                        Console.WriteLine("星期一");

                        break;

                }

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

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

    例:输入两个时间日期,计算出相差多少天(TotalDays)
                Console.Write("请输入你们恋爱的时间:");
                DateTime dt = DateTime.Parse(Console.ReadLine());
                DateTime dt1 = DateTime.Now;
                Console.WriteLine((dt1-dt).TotalDays);

    try   catch 异常保护语句
                Console.Write("请输入一个整数:");
                try//尝试
                {
                    int a = int.Parse(Console.ReadLine());
                    Console.WriteLine(a);
                }
                catch//若try里面的语句有问题,直接跳到catch执行
                {
                    Console.WriteLine("程序出现错误!");
                }
                //finally//不管对与错,都要执行
                //{
                //    Console.WriteLine("感谢您的使用!");
                //}
                Console.WriteLine("感谢您的使用!");

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

                Console.ReadLine();

    例:输入时间  错误重新输入 循环
                Console.Write("请输入日期:");
                string a = Console.ReadLine();
                for (int i = 0; i < 1; i++)
                {
                    try
                    {
                        DateTime dt = new DateTime();
                        dt = DateTime.Parse(a);
                        Console.WriteLine("输入正确");
                    }
                    catch
                    {
                        Console.WriteLine("输入错误,请重新输入");
                        a = Console.ReadLine();
                        i--;
                    }
                }
                Console.ReadLine();

  • 相关阅读:
    @bzoj
    @bzoj
    @codeforces
    @codeforces
    @bzoj
    @codeforces
    @codeforces
    @codeforces
    @NOIP2018
    反转字符串--C和Python
  • 原文地址:https://www.cnblogs.com/ShenG1/p/5617136.html
Copyright © 2020-2023  润新知