zoukankan      html  css  js  c++  java
  • 5月7日 类

    一、String  类:S是大写,大String是父级,小string是子级

    1、 .Length 字符串的长度

                string s = "Hello";   

                int a = s.Length;//字符串的长度

                Console.WriteLine(a);//所以a等于5

    2.Trim() 去掉开头以及结尾的空格

    string s = "     Hello     ";           

                Console.Write(s.Trim());

                Console.WriteLine("123");

     

    3、 .TrimStart()   去掉字符串开头的空格

    string s = "     Hello     ";

                Console.Write(s.TrimStart());

                Console.WriteLine("123");

     

    4.TrimEnd()   去掉字符串后面的空格

    string s = "     Hello     ";

                Console.Write(s.TrimEnd());

                Console.WriteLine("123");

     

    5.ToUpper() 全部大写

    string s = "     Hello     ";

                Console.WriteLine(s.ToUpper());

     

    6、 .ToLower() 全部小写

    string s = "     Hello     ";

                Console.WriteLine(s.ToLower());

     

    7Substring(起始位置,截取长度)

       Substring(起始位置) 只写起始位置,可以截取到尾

    //Substring     截取字符串

                //只有一个值的时候,开始索引号

                string ss = "37030319900305330";

                //所有的字符串的索引都是从0开始的

                Console.WriteLine(ss.Substring(3));

                //放置两个值(开始索引,截取长度)

                //截取出来身份证的年份

                int year = int.Parse(ss.Substring(6, 4));

                Console.WriteLine(year);

     

    8、小练习

    //输入身份证号,截取生日出来并打印

                //Console.Write("请输入身份证号:");

                //string cid = Console.ReadLine();

                //Console.WriteLine("您的生日是:"+cid.Substring(6,4)+"年"+cid.Substring(10,2)+"月"+cid.Substring(12,2)+"日。");

     

    9//IndexOf("字符串") 返回第一次出现此字符串的索引

                string sss = "abcdefghijefgklmn";

                int a = sss.IndexOf("efg");

                //返回值若为-1,表示未找到

                Console.WriteLine(a);

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

                string sss = "abcdefghijefgklmn";

                int b = sss.LastIndexOf("efg");

                Console.WriteLine(b);

    10//StartsWith("字符串") 是否以此字符串为开头,返回True或False

                string sss = "abcdefghijefgklmn";

                bool c = sss.StartsWith("abc");

                //EndsWith("字符串") 是否以此字符串为结尾

                bool d = sss.EndsWith("abc");

                Console.WriteLine(c);

                Console.WriteLine(d);

    11//Contains("字符串") 是否包含此字符串。返回True或者False

                string sss = "abcdefghijefgklmn";

                bool e = sss.Contains("lt");

                Console.WriteLine(e);

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

                sss = sss.Replace("efg", "你好");

                Console.WriteLine(sss.Replace("efg", "你好"));

                Console.WriteLine(sss);

                Console.ReadLine();

    13、练习:

    //练习:判断邮箱格式是否正确

                //1.有且只能有一个@

                //2.不能以@开头

                //3.@之后至少有一个.

                //4.@和.不能靠在一起

                //5.不能以.结尾

                Console.Write("请输入邮箱账号:");

                string mail = Console.ReadLine();

                if (mail.Contains("@"))

                {

                    int aa = mail.IndexOf("@");

                    int bb = mail.LastIndexOf("@");

                    if (aa == bb)

                    {

                        bool a = mail.StartsWith("@");

                        if (!a)

                        {

                            string ss = mail.Substring(aa);

                            if (ss.Contains("."))

                            {

                                int dian = ss.IndexOf(".");

                                if (dian != 1)

                                {

                                    int hou = ss.LastIndexOf(".");

                                    if (hou != ss.Length - 1)

                                    {

                                        Console.WriteLine("输入邮箱的格式正确!");

                                    }

                                    else

                                    {

                                        Console.WriteLine("输入有误!");

                                    }

                                }

                                else

                                {

                                    Console.WriteLine("输入有误!");

                                }

                            }

                            else

                            {

                                Console.WriteLine("输入有误!");

                            }

                        }

                        else

                        {

                            Console.WriteLine("输入有误!");

                        }

                    }

                    else

                    {

                        Console.WriteLine("输入有误!");

                    }

                }

                else

                {

                    Console.WriteLine("输入有误!");

                }

    二、Math类:数学类

    1//Ceiling() 取上线

                double a = 4.14;

                double b = Math.Ceiling(a);

                Console.WriteLine(b);

    2//Floor() 取下线

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

                Console.ReadLine();

    3//Math.PI 圆周率

                double r = 3;

                Console.WriteLine(Math.PI*r*r);

    4//Math.Sqrt() 平方根

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

    5//Math.Round() 四舍五入(注意奇数偶数下.5不一样的结果)

                Console.WriteLine(Math.Round(4.5));

                //奇数.5的情况下,默认取上线

                //偶数.5的情况下,默认取下线

    三、//DateTime类:

                //注意在使用之前需要先初始化一遍。

                //DateTime dt =new DateTime();

                //DateTime dt = DateTime.Now;//系统当前时间,运行时查询

                //Console.WriteLine(dt.Year);

                //获取年 dt.Year

                //获取月 dt.Month

                //获取日 dt.Day

                //获取小时 dt.Hour

                //获取分 dt.Minute

                //获取秒 dt.Second

                //获取这一天是星期几

                //DayOfWeek d = dt.DayOfWeek;

                //获取到的是英文。

                //若想用中文,先d.ToString()

                //然后根据英文打印出中文。

                //DayOfWeek d = dt.DayOfWeek;

                //Console.WriteLine(d);

                //if (d.ToString() == "Saturday")

                //{

                //    Console.WriteLine("星期六");

                //}

                //string name = "张三";

                //Console.WriteLine("你的名字是:{0}",name);

                //Console.WriteLine( dt.AddDays(3.5));

                //Console.WriteLine(dt);

                //时间间隔使用TimeSpam定义

                //TimeSpan ts = new TimeSpan(3, 3, 3, 3);

                //Console.WriteLine( dt.Add(ts));

    四、//Random   随机数类

                //使用前需要进行初始化

                Console.Write("请输入第一个姓名:");

                string name1 = Console.ReadLine();

                Console.Write("请输入第二个姓名:");

                string name2 = Console.ReadLine();

                Random ran = new Random();//初始化

                int a = ran.Next(101);

                if (a < 60)

                {

                    Console.WriteLine("{0}和{1}的缘分指数为:{2},趁早散伙吧!", name1, name2, a);

                }

                else if (a < 80)

                {

                    Console.WriteLine("{0}和{1}的缘分指数为:{2},加油努力还可能行!", name1, name2, a);

                }

                else

                {

                    Console.WriteLine("{0}和{1}的缘分指数为:{2},般配!!", name1, name2, a);

                }

                Console.ReadLine();

  • 相关阅读:
    移动端网页 -- 安卓与IOS兼容
    item上下自动循环滚动显示
    刮刮奖 --- 可以自定义在图层下添加文字等等信息
    内核终端判断,微信?QQ?ipad?IE?移动?Google?opera……
    flexbox布局
    刮刮奖
    constructor
    获取地址栏参数
    this对象
    函数
  • 原文地址:https://www.cnblogs.com/juyangchao12/p/5471374.html
Copyright © 2011-2022 走看看