你的位置:首页 > 软件开发 > ASP.net > C#语言——类

C#语言——类

发布时间:2016-06-25 23:01:01
C#——类一、String 类系统内置的处理字符串类型的函数方法类。方便我们对字符串类型进行一系列的处理。1、Length:获取字符串的长度,返回一个int类型的值string x=Console.ReadLine();//小string是 ...

C#——类

一、String 类

系统内置的处理字符串类型的函数方法类。方便我们对字符串类型进行一系列的处理。

1、Length:获取字符串的长度,返回一个int类型的值

get='_blank'>string x=Console.ReadLine();//小string是大String的快捷方式

int i = x.Length; Console.Write(i);

Console.ReadLine();

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

                       

 

 

 

3、.ToUpper() 全部大写;.ToLower()    全部小写。

 

4、Substring(起始位置,截取长度);Substring(起始位置)  只写起始位置,可以截取到尾。

注:字符串的编码索引是从0开始的。

 

5、IndexOf("字符串")    返回第一次出现此字符串的索引,查找开头第一次出现的索引号,返回值为-1.表示没有找到。

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

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

EndsWith("字符串")   是否以此字符串为结尾,返回True或False。

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

7、Replace("老字","新字")   将老字用新字替换,即替换所有符合指定段的字符串条件的字符串(查找替换功能)。

综上例题:判断邮箱格式是否正确: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("."))

                        {

                            //731944381@qq.com

                            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();

法二、      Console.Write("请输入一个邮箱:");

            string s = Console.ReadLine();

            bool j = s.Contains("@");

            int a = s.IndexOf("@");

            int b = s.LastIndexOf("@");

            bool i = true;

            bool c = s.StartsWith("@");

            string d = s.Substring(a);

            bool e = d.Contains(".");

            int f = s.IndexOf(".");

            int g = s.LastIndexOf(".");

            bool h = s.EndsWith(".");

            if (j == i && a == b && f != a - 1 && g != b - 1 && c != i && e == i && h != i)

            {

                Console.WriteLine("格式正确");

            }

            else

            {

                Console.WriteLine("格式错误");

            }

            Console.ReadLine();

二、Math 类

●Ceiling()            取上线

●Floor()                     取下线

●Math.PI                    圆周率

●Math.Sqrt()        平方根

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

 

三、随机数类:Random

需要使用随机数的时候需要先初始化

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

            int a = ran.Next(10);//返回0-9范围内的数

            Console.WriteLine(a);

综上例题:验证码:随机出四位验证码,A~Z    a~z    0~9,不区分大小写。

法一、      string ss = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

            Random s = new Random();

            int a = s.Next(62);

            int b = s.Next(62);

            int c = s.Next(62);

            int d = s.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 ee = aa + bb + cc + dd;

            Console.WriteLine("验证码是:" + ee);

            Console.Write("请对照输入验证码:");

            string shu = Console.ReadLine();

            if (shu.ToUpper() == ee.ToUpper())

            {

                Console.WriteLine("输入正确!");

            }

            else

            {

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

            }

            Console.ReadLine();

法二、      string ss = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

            Random s = new Random();

            string yan = "";

            for (int i = 1; i <= 4; i++)

            {

                int a = s.Next(62);

                yan += ss.Substring(a);

 

            }

            Console.WriteLine("验证码是:" + yan);

            Console.Write("请对照输入验证码:");

            string shu = Console.ReadLine();

            if (shu.ToUpper() == yan.ToUpper())

            {

                Console.WriteLine("输入正确!");

            }

            else

            {

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

            }

            Console.ReadLine();

四、DateTime类  获取时间

1、●若需要使用,首先需要初始化

            DateTime dt = new DateTime();

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

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

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

            DateTime dt1 = DateTime.Now;

            Console.WriteLine(dt1);

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

●DayOfYear 获取日期是当年的第几天,返回int类型值

 

 

2、在控制台输入的格式必须符合DateTime的格式才能正确接收

 

3、s = dt.ToString("yyyy年MM月dd日hh时mm分ss秒");

yyyy MM dd hh mm ss 均为代位符

yyyy年,MM月(必须大写),dd日,hh时,mm分(小写),ss(秒)

 

4、DateTime可以增加或者减去相应的时间

Add()             增加或者减去一定的时间间隔

AddYears()   增加或减去年份

AddMonths()        增加或减去月份

AddDays()    增加或减去天数

以此类推。

注意,加减天数,小时数是利用double类型。其他都是int类型

 

 

 

5、TimeSpan

 

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

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

6、 获取年                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;

            }

例题:输入两个时间日期,计算出相差多少天(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("感谢您的使用!");

例题:判断输入的年月日格式是否正确

法一、DateTime

            Console.Write("请按照格式正确输入年月日(yyyy/MM/dd)");

            string td = Console.ReadLine();

            DateTime dt = new DateTime();

            dt = DateTime.Parse(td);

            string s;

            s = dt.ToString("yyyy年MM月dd日");

            string y = s.Substring(0, 4);

            string M = s.Substring(5, 2);

            string d = s.Substring(8, 2);

            int iy = int.Parse(y);

            int iM = int.Parse(M);

            int id = int.Parse(d);

            int rm = 1;

            if (iy % 100 == 0)

            {

                if (iy % 400 == 0)

                {

                    rm = 4;

                }

            }

            else if (iy % 4 == 0)

            {

                rm = 4;

            }

            if (iy > 1 && iy <= 9999 && iM >= 1 && iM <= 12 && id >= 1 && id <= 31)

            {

                if (iM == 4 || iM == 6 || iM == 9 || iM == 11 && id >= 30)

                {

                    Console.Write("输入错误");

                }

                else if (iM == 2 && id == 29 && rm != 4)

                {

                    Console.Write("输入错误!:" + iy + "年不是闰年,2月没有29号");

                }

                else if (iM == 2 && id >= 29)

                {

                    Console.Write("输入错误");

                }

                else

                {

                    Console.WriteLine(s);

                    Console.WriteLine("输入正确!");

                }

            }

            Console.ReadLine();

法二、try-catch异常语句

            Console.Write("请输入日期时间:");

            try

            {

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

                Console.WriteLine("您输入的日期时间格式正确!");

            }

            catch

            {

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

            }

            Console.WriteLine("感谢您的使用!再见!");

            Console.ReadLine();

 

 

综合例题:1、输入一个1-100内的整数,若输入错误请重新输入

法一、      Console.Write("请输入一个数:");

            int a = int.Parse(Console.ReadLine());

            for (int i = 1; ; i++)

            {

                if (a <= 100 && a >= 1)

                {

                    Console.WriteLine(a);

                    break;

                }

                else

                {

                    Console.Write("请重新输入一个数:");

                    a = int.Parse(Console.ReadLine());

                }

            }

            Console.ReadLine();

法二        Console.Write("请输入一个数:");

            int a = int.Parse(Console.ReadLine());

            for (int i = 0; i<1; i++)

            {

                if (a <= 100 && a >= 1)

                {

                    Console.WriteLine(a);

                }

                else

                {

                    Console.Write("请重新输入一个数:");

                    a = int.Parse(Console.ReadLine());

                    i--;

                }

            }

            Console.ReadLine();

2、输入年月日,判断日期格式是否正确,不正确,重新输入。

            Console.Write("请输入年份:");

            int a = int.Parse(Console.ReadLine());

            for (int i = 0; i < 1; i++)

            {

                if (a >= 0 && a <= 9999)

                {

                    Console.Write("请输入月份:");

                    int b = int.Parse(Console.ReadLine());

                    for (int j = 0; j < 1; j++)

                    {

                        if (b > 0 && b <= 12)

                        {

                            Console.Write("请输入日:");

                            int c = int.Parse(Console.ReadLine());

                            for (int k = 0; k < 1; k++)

                            {

                                if (c > 0 && c <= 31)

                                {

                                    if (b == 1 || b == 3 || b == 5 || b == 7 || b == 8 || b == 10 || b == 12)

                                    {

                                        Console.WriteLine("您输入的日期格式正确!输入的日期是:{0}-{1}-{2}", a, b, c);

                                    }

                                    else if (b == 4 || b == 6 || b == 9 || b == 11)

                                    {

                                        for (int l = 0; l < 1; l++)

                                        {

                                            if (c <= 30)

                                            {

                                                Console.WriteLine("您输入的日期格式正确!输入的日期是:{0}-{1}-{2}", a, b, c);

                                            }

                                            else

                                            {

                                                Console.Write("请重新输入日份:");

                                                c = int.Parse(Console.ReadLine());

                                                l--;

                                            }

                                        }

                                    }

                                    else

                                    {

                                        if (c <= 28)

                                        {

                                            Console.WriteLine("您输入的日期格式正确!输入的日期是:{0}-{1}-{2}", a, b, c);

                                        }

                                        else

                                        {

                                            for (int r = 0; r < 1; r++)

                                            {

                                                if (c == 29)

                                                {

                                                    for (int t = 0; t < 1; t++)

                                                    {

                                                        if ((a % 4 == 0 && a % 100 != 0) || a % 400 == 0)

                                                        {

                                                            Console.WriteLine("您输入的日期格式正确!输入的日期是:{0}-{1}-{2}", a, b, c);

                                                        }

                                                        else

                                                        {

                                                            Console.Write("请重新输入年份:");

                                                            a = int.Parse(Console.ReadLine());

                                                            t--;

                                                        }

                                                    }

                                                }

                                                else

                                                {

                                                    Console.Write("请重新输入日份:");

                                                    c = int.Parse(Console.ReadLine());

                                                    r--;

                                                }

                                            }

                                        }

                                    }

                                }

                                else

                                {

                                    Console.Write("请重新输入日份:");

                                    c = int.Parse(Console.ReadLine());

                                    k--;

                                }

                            }

                        }

                        else

                        {

                            Console.Write("请重新输入月份:");

                            b = int.Parse(Console.ReadLine());

                            j--;

                        }

                    }

                }

                else

                {

                    Console.Write("请重新输入年份:");

                    a = int.Parse(Console.ReadLine());

                    i--;

                }

            }

            Console.ReadLine();


原标题:C#语言——类

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录