【2017-2-27】三大类

(一)string类

 

1.字符串.length----字符串长度,返回int类型

2.字符串.TrimStart()   -----  去掉前空格          返回string类型

   字符串.TrimEnd()  ------   去掉后空格    返回string类型

    字符串.Trim()  ----------  去掉前后空格    返回string类型

3.字符串.SubString(a); -----   截取字符串  包括a以后的所有字符   返回string类型

   字符串.SubString(a,b);  ------    截取字符串 a 表示下标  b表示要截取的字符串的个数

    注意:a下标不能超过字符串的长度,b的长度也不可以

4.字符串.Indexof("串")  ---- 返回字符串中第一个匹配的索引,如果没有返回int -1;

     int b = s.Indexof("天",s.Indexof("天")+1);   获得第二个匹配项

5.字符串.LastIndexOf("串"); - 返回最后一个匹配项的索引

6.字符串.StartWidth("串"); - 判断是否以什么开头               返回string类型

 字符串.EndsWidth("串"); - 判断是否以什么结尾                 返回string类型
 字符串.Contains("串"); - 判断是否包含                             返回 string类型

7.  s.Replace(要替换的字符串, 替换的字符串); - 字符替换 string

     s.Remove(3); - 移除从索引到末尾的全部字符 string

 

 

(二)Math类

 

Math.Pow(x,y); - 次方
Math.Sqrt(x); - 平方根

Math.Ceiling(double); - 取上限
Math.Floor(double); - 取下限
Math.Round(double); - 取四舍五入 当整数部分为奇数5上位,整数部分位偶数5舍去
Math.Abs(double); - 取绝对值

 

 

(三)DateTime类

 

DateTime.Now; - 获取此电脑当前时间

.ToString("Format"); - 设置日期格式化,
yyyy-年 MM-月 dd-日 hh-12制小时 HH-24制小时 mm-分钟 ss-秒 ms-毫秒

.AddYears(); - 在此时间基础上增加多少年
.AddMonths(); - 增加月
.AddDays(); - 增加日
.AddHours(); - 增加小时
.AddMinutes(); - 增加分钟
.AddSeconds(); - 增加秒

.Year; - 获取此时间变量的年份
.Month; - 获取月份
.Day; - 日
.Hour; - 小时
.Minute; - 分钟
.Second; - 秒
.Millisecond; - 毫秒

.DayOfYear; - 获取当前日期是此年中的第几天
.DayOfWeek; - 获取是星期几

.TimeOfDay; - 获取时间部分
.Date; - 获取日期部分


TimeSpan类型 - 时间间隔类型
.Days - 差距多少天
.Hours - 一天中差距多少小时
.Minutes - 一天中差距多少分钟
.Seconds - 一天中差距多少秒
.Milliseconds - 毫秒

.Total.... 累计差距

 

 

练习题

---------------------------------------------------------------------

“请输入您的邮箱:”123@123
 1-“邮箱正确!/错误!”
 2-“只能有一个@符号”
 3-“不能以@开头”
 4-“不能以@结尾”
 5-“@之后必须有点”
 6-“@之后不能是点”
 7- @之后最少一个点,最多两个点
 8-“不能以点结尾”
 9-不能以数字结束

while (true)
            {

                //1-“邮箱正确!/错误!”    string 放置最终结果
                string end = "邮箱正确!";
                //2-“只能有一个@符号”      bool
                bool atOnlyOne = true;
                //3-“不能以@开头”          bool
                bool atStart = true;
                //4-“不能以@结尾”          bool
                bool atEnd = true;
                //5-“@之后必须有点”        bool
                bool atDian = true;
                //6-“@之后不能是点”         bool
                bool atNoDian = true;
                //7-最少一个点,最多两个点   
                bool dianOneOrTwo = true;
                //8-“不能以点结尾”
                bool dianEnd = true;
                //9-不能以数字结束
                bool NumEnd = true;

                //一、让用户输入邮箱
                Console.Write("请输入您的邮箱地址:");
                string user_mail = Console.ReadLine();

                if (user_mail.Length > 0)
                {
                    #region 是否只有一个@符号
                    int a1 = user_mail.IndexOf("@");

                    if (a1 == -1)
                    {
                        atOnlyOne = false;
                        end = "邮箱格式错误!";
                    }
                    else
                    {
                        int a2 = user_mail.IndexOf("@", a1 + 1);
                        if (a2 != -1)
                        {
                            atOnlyOne = false;
                            end = "邮箱格式错误!";
                        }
                    }
                    #endregion

                    #region 判断是否以@开头
                    if (user_mail.StartsWith("@"))
                    {
                        atStart = false;
                        end = "邮箱格式错误!";
                    }
                    #endregion

                    #region 判断是否以@结尾
                    if (user_mail.EndsWith("@"))
                    {
                        atEnd = false;
                        end = "邮箱格式错误!";
                    }
                    #endregion

                    #region 判断@之后有没有点,判断如果有点,是不是超过两个
                    string a3 = user_mail.Substring(user_mail.IndexOf("@"));
                    int a4 = a3.IndexOf(".");
                    if (a4 == -1)
                    {
                        atDian = false;
                        end = "邮箱格式错误!";
                    }
                    else
                    {
                        //@sina.com.cn
                        int a6 = 1; //记录点的个数
                        int a5 = a3.IndexOf("."); //获取第一个点的索引

                        while (true)
                        {
                            a5 = a3.IndexOf(".", a5 + 1);//持续往后找点
                            if (a5 != -1)
                                a6++;
                            else
                                break;
                        }

                        if (a6 > 2)
                        {
                            dianOneOrTwo = false;
                            end = "邮箱格式错误!";
                        }
                    }
                    #endregion

                    #region @之后不能是点
                    if (user_mail.IndexOf("@.") != -1)
                    {
                        atNoDian = false;
                        end = "邮箱格式错误!";
                    }
                    #endregion

                    #region 不能以点结尾
                    if (user_mail.EndsWith(".") == true)
                    {
                        dianEnd = false;
                        end = "邮箱格式错误!";
                    }
                    #endregion

                    #region 不能以数字结束
                    string a10 = user_mail.Substring(user_mail.Length - 1, 1);
                    try
                    {
                        Convert.ToInt32(a10);
                        NumEnd = false;
                        end = "邮箱格式错误!";
                    }
                    catch { }
                    #endregion

                    #region 打印结果
                    //打印结果!!!
                    if (atOnlyOne == false)
                        Console.WriteLine("只能有一个@符号");
                    if (atStart == false)
                        Console.WriteLine("不能以@开头");
                    if (atEnd == false)
                        Console.WriteLine("不能以@结尾");
                    if (atDian == false)
                        Console.WriteLine("@之后必须有点");
                    if (atNoDian == false)
                        Console.WriteLine("@之后不能是点");
                    if (dianOneOrTwo == false)
                        Console.WriteLine("最少一个点,最多两个点");
                    if (dianEnd == false)
                        Console.WriteLine("不能以点结尾");
                    if (NumEnd == false)
                        Console.WriteLine("不能以数字结束");

                    Console.WriteLine(end);
                    #endregion
                }
                else
                {
                    Console.WriteLine("邮箱不能为空!");
                }

            }
            Console.ReadKey();

 

“请输入身份证号(18位):”
 判断正确性:
 全数字
 最后一位x/X
 中间时间是否正确
 
 “您的生日是:xxxx年xx月xx日”

Console.Write("请输入您的身份证号:");
            string user = Console.ReadLine();

            //判断是否是18位
            if (user.Length == 18)
            {
                string user17 = user.Substring(0, user.Length - 1);

                try
                {
                    Convert.ToInt64(user17);
                    //如果能走到这里,说明没问题
                    string userLast = user.Substring(user.Length - 1, 1);
                    bool LastIsNumber = true;
                    try
                    {
                        Convert.ToInt32(userLast);
                    }
                    catch { LastIsNumber = false; }

                    //判断最后一位是否正确
                    if (userLast.ToLower() == "x" || LastIsNumber)
                    {
                        //如果走到这里,说明格式都正确
                        //判断日期是否正确
                        string year = user.Substring(6, 4);
                        string month = user.Substring(10, 2);
                        string day = user.Substring(12, 2);

                        try
                        {
                            DateTime dt = new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(day));

                            Console.WriteLine("您的生日是:"+dt.ToString("yyyy年MM月dd日"));
                        }
                        catch { Console.WriteLine("身份证日期不正确!"); }
                    }
                    else
                    {
                        Console.WriteLine("输入的格式不正确!1");
                    }
                }
                catch
                {
                    Console.WriteLine("输入的格式不正确!2");
                }
            }
            else
                Console.WriteLine("身份证位数不正确!3");

            Console.ReadKey();

 

“请输入年:”
“请输入月:”
“请输入日:”

判断是否正确

“xxxx年xx月xx日是此年中的第xx天,星期几”

“距离2012年12月24日世界末日还有xxx天”
“距离2012年12月24日世界末日已经过去了xxx天”
“您输入的是世界末日!!!”

static void Main(string[] args)
        {
            Console.Write("请输入年:");
            string year = Console.ReadLine();
            Console.Write("请输入月:");
            string month = Console.ReadLine();
            Console.Write("请输入日:");
            string day = Console.ReadLine();

            try
            {
                DateTime dt = new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(day));

                string week = "星期" + "日一二三四五六".Substring(Convert.ToInt32(dt.DayOfWeek), 1);
                Console.WriteLine(dt.Year + "" + dt.Month + "" + dt.Day + "日是此年中的第" + dt.DayOfYear + "天," + week);

                DateTime dtt = new DateTime(2012, 12, 24);

                TimeSpan ts = dt - dtt;

                int ddd = Convert.ToInt32(ts.TotalDays);

                if (ddd < 0)
                {
                    Console.WriteLine("距离2012年12月24日世界末日还有" + Math.Abs(ddd) + "");
                }
                else if (ddd > 0)
                {
                    Console.WriteLine("距离2012年12月24日世界末日已经过去了" + ddd + "");
                }
                else
                {
                    Console.WriteLine("您输入的是世界末日!!!");
                }
            }
            catch { Console.WriteLine("你填的些啥???"); }



            Console.ReadLine();

 

posted @ 2017-03-02 21:57  Fengbao.2333  阅读(226)  评论(0编辑  收藏  举报