【2-26】string/math/datetime类的定义及其应用

string

(1)字符串.Length    Length作用于求字符串的长度,返回一个int值

(2)字符串.TrimStart();  TrimStart();可删除前空格,返回一个string类

(3)字符串.TrimEnd();   TrimEnd(); 可删除后空格,也返回一个string类

(4) ***字符串.Trim();      Trim(); 删除前后空格,返回string类

字母大小写:

(5)****字符串.ToUpper();  ToUpper();  将字符串大写,返回string类

(6)****字符串.ToLower();  ToLower( ); 将字符串小写,返回string类

截取:

(7)***字符串.SubString(a);  将字符串从下标a开始截取后面部分

(8)****字符串.SubString(a,b);  将字符串从下标a开始截取后面b个字符,返回string类

注意:下标索引从0开始

截取的对象下标、长度必须在字符串范围之内,否则报错。

索引:

(9)***字符串.IndexOf(“字符串a”);  从字符串中索引第一个字符串a,返回int值

(10)****字符串.IndexOf(“字符串a”,int b );  从b值开始索引字符串a, 返回int值

(11)***字符串.LastIndexOf(“字符串a”);  从最后开始索引,索引出最后一个字符串a,    返回int值

注意:如果字符串中不存在索引对象,则返回-1;

比较判断字符:

(12)***字符串.StartWidth("字符串a");  判断字符串是否以“字符串a”开头  

(13)***字符串.EndsWidth("字符串b"); - 判断字符串是否以“字符串b”结尾

(14)**字符串.Contains("字符串c"); - 判断字符串是否包含“字符串c”,返回bool类

移除、替换:

(15)****变量.Replace(要替换的字符串a,替换成的字符串b);  字符替换  返回string类

(16)s.Remove(索引int值); 移除从索引到末尾的全部字符  返回string类

例:

Console.Write("请输入您的邮箱:");
            string id = Console.ReadLine();
            string end = "输入的邮箱正确";
            bool mailonly = true;
            bool mailonlyy = true;
            bool mailstart = true;
            bool mailend = true;
            bool atyd = true; 
            bool atsd = true;
            bool dianot = true;
            bool dianno = true;
            bool numno = true;
            int s = id.Length;

#region 有且只有一个@符号
            int mailonly1 = id.IndexOf("@");
            if (mailonly1 == -1)
            {
                mailonlyy = false;
                end = "输入的邮箱有误!";
            }
            else
            {
                string mailonly2 = id.Substring(mailonly1+1);
            int mailonly3 = mailonly2.IndexOf("@");
            if (mailonly3 != -1)
            {
                mailonly = false;
                end = "输入的邮箱有误!";
            }
            }
#endregion

#region 不能以@开头

            bool mailstart1 = id.StartsWith("@");
            if (mailstart1 == true)
            {
                mailstart = false;
                end = "输入的邮箱有误!";
            }
#endregion

#region 不能以@结尾
            bool mailend1 = id.EndsWith("@");
            if (mailend1 == true)
            {
                mailend = false;
                end = "输入的邮箱有误!";
            }
#endregion

#region@之后必须有点
            if (mailonly1 != -1)
            {
                string atyd1 = id.Substring(mailonly1);
                int atyd2 = atyd1.IndexOf(".");
                if (atyd2 == -1)
                {
                    atyd = false;
                    end = "输入的邮箱有误!";
                }
            }
            else
            {
                end = "输入的邮箱有误!";
            }
#endregion

#region@之后不能是点
            int atsd1 = id.IndexOf("@.");
            if (atsd1 != -1)
            {
                atsd = false;
                end = "输入的邮箱有误!";
            }
            #endregion

#region 最少一个点,最多两个点
            for (; ; )
            {
                try
                {
                    string atyd1 = id.Substring(mailonly1);
                    int dianot1 = atyd1.IndexOf(".");
                    int a = 0;
                    if (dianot1 != -1)
                    {
                        a++;
                    }
                    if (a > 2)
                    {
                        dianot = false;
                        end = "输入的邮箱有误!";
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
                catch
                {
                    end = "输入的邮箱有误!";
                    break;
                }
            }
#endregion

#region 不能以点结尾

            bool dianno1 = id.EndsWith(".");
            if (dianno1 == true)
            {
                dianno = false;
                end = "输入的邮箱有误!";
            }

#endregion

 #region 不能以数字结束

            string numno1 = id.Substring(s-1);
            try
            {
                int numno2 = Convert.ToInt32(numno1);
                bool b=Convert.ToBoolean(numno2);
                if (b == true)
                {
                    numno = false;
                    end = "输入的邮箱有误!";
                }
            }
            catch 
            {
            }
 #endregion
            if (mailonlyy == false)
            {
                Console.WriteLine("必须有@");
            }
            if (mailonly == false)
            {
                Console.WriteLine("只能有一个@符号");
            }
            if (mailstart == false)
            {
                Console.WriteLine("不能以@开头");
            }
            if (mailend == false)
            {
                Console.WriteLine("不能以@结尾");
            }
            if (atyd == false)
            {
                Console.WriteLine("@之后必须有点");
            }
            if (atsd == false)
            {
                Console.WriteLine("@之后不能是点");
            }
            if (dianot == false)
            {
                Console.WriteLine("最少一个点,最多两个点");
            }
            if (dianno==false)
            {
                Console.WriteLine("不能以点结束");
            }
            if (numno==false)
            {
                Console.WriteLine("不能以数字结束");
            }
            Console.WriteLine(end);
            Console.ReadLine();

math

(1)Math.Pow(变量值x,幂y); 求变量值x的y幂次方;x,y为double类型,返回double类

(2)Math.Sqrt(变量值x);  求变量值x的平方根,返回double类

(3)Math.Ceiling(变量值x);   取变量值X(double类)上限

(4)Math.Floor(变量值x);    取变量值x(double类)下限

(5)Math.Round(变量值x);  四舍五入,当整数部分为奇数5入;当整数部分为奇数5舍

(6)Math.Abs(变量值x);     取变量值x的绝对值

三DateTime

(1)DateTime 变量名 = new DateTime(); - 定义变量类型

变量名 = DateTime.Now; - 获取此电脑当前时间

(2)变量.ToString("格式"); - 设置日期格式化

格式:yyyy-年 MM-月 dd-日 hh-12制小时 HH-24制小时 mm-分钟 ss-秒  ms-毫秒

(3)时间加

1、变量.AddYears(int值); - 在此时间基础上增加int值年

2、变量.AddMonths(int值); - 在此时间基础上增加int值月

3、变量.AddDays(int值); - 在此时间基础上增加int值日

4、变量.AddHours(); - 在此时间基础上增加int值小时

5、变量.AddMinutes(); - 在此时间基础上增加int值分钟

6、变量.AddSeconds(); - 在此时间基础上增加int值秒

(4)截取部分时间变量

1、时间变量.Year; - 获取此时间变量的年份

2、时间变量.Month; - 获取月份

3、时间变量.Day; - 日

4、时间变量.Hour; - 小时

5、时间变量.Minute; - 分钟

6、时间变量.Second; - 秒

7、时间变量.Millisecond; - 毫秒

5)获取变量天数、星期

1、时间变量.DayOfYear; - 获取当前日期是此年中的第几天

2、时间变量.DayOfWeek; - 获取是星期几

3、时间变量.TimeOfDay; - 获取时间部分

4、时间变量.Date; - 获取日期部分

(6)TimeSpan类型 - 时间间隔类型

1、时间变量.Days - 差距多少天

2、时间变量.Hours - 一天中差距多少小时

3、时间变量.Minutes - 一天中差距多少分钟

4、时间变量.Seconds - 一天中差距多少秒

5、时间变量.Milliseconds - 毫秒

6、时间变量.Total(Days、Hours、Minutes…)累计天、小时等差距

例:

Console.Write("请输入年:");
            string year = Console.ReadLine();
            Console.Write("请输入月:");
            string month = Console.ReadLine();
            Console.Write("请输入日:");
            string day = Console.ReadLine();
            string s = year + "/" + month + "/" + day;
            try
            {
                DateTime dt = new DateTime();
                dt = DateTime.Parse(s);
                string a = dt.ToString("yyyy年MM月dd日");
                int b = Convert.ToInt32(dt.DayOfWeek);
                int c = dt.DayOfYear;
                string x = "日一二三四五六";
                string x1 = x.Substring(b, 1);
                Console.WriteLine(a + "是此年的第" + c + "天,星期" + x1);
                DateTime y = new DateTime(2012, 12, 24);
                TimeSpan t = dt - y;
                int t1 = Convert.ToInt32(t.Days);
                int t2 = Math.Abs(t1);
                if (t1 < 0)
                {
                    Console.Write("距离2012年12月24日世界末日还有" + t2 + "");
                }
                else if (t1 > 0)
                {
                    Console.Write("世界末日过去了" + t1 + "");
                }
                else
                {
                    Console.Write("您输入的是世界末日!!!");
                }

            }
            catch
            {
                Console.WriteLine("输入的时间日期有误!");
            }

            Console.ReadLine();
原文地址:https://www.cnblogs.com/hclyz/p/6489689.html