• 2017-2-27 C#基础 三大类


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

    字符串.TrimStart() - 去掉前空格
    字符串.TrimEnd() - 去掉后空格
    字符串.Trim() - 去掉字符串的前后空格 string

    字符串.ToUpper() - 将字符串中的小写字符变成大写 string
    字符串.ToLower() - 变成小写 string

    索引/下标
    字符串.SubString(a); - 截取字符串,a - 要开始截取的下标,包含下标所对应的字符
    字符串.SubString(a,b); - a - 下标 , b - 要截取几个字符(从1开始数)
    string

    字符串.IndexOf("串"); - 返回字符串中第一个匹配项的索引,如果没有匹配项返回-1 int
    int b = s.IndexOf("天",s.IndexOf("天")+1); //获得第二个匹配项,3 4 5 6

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

    字符串.StartWidth("串"); - 判断是否以什么开头
    字符串.EndsWidth("串"); - 判断是否以什么结尾
    字符串.Contains("串"); - 判断是否包含 string

    s.Replace(要替换的字符串, 替换的字符串); - 字符替换 string
    s.Remove(3); - 移除从索引到末尾的全部字符 string

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

    Math.Ceiling(double); - 取上限
    Math.Floor(double); - 取下限
    Math.Round(double); - 取四舍五入
    Math.Abs(double); - 取绝对值

    +++++DateTime类+++++
    DateTime 变量名 = new 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.... 累计差距

    String.split()

    练习题:

    用户输入一个数字
    1,2,3,4,5,6

    namespace _2017_2_26__不输出逗号
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("请输入一个数字");
                int a = Convert.ToInt32(Console.ReadLine());
                String end = "";
                for (int b = 1; b <= a; b++) 
                {
                  end += b + ",";
                }
                String c = end.Substring(0,end.Length-1);
                Console.Write(c);
                Console.ReadLine();
    
            }
        }
    }

    1、“验证码:xxxx”
    “请输入验证码:”
    “输入的位数不正确!”
    大小写都行
    “验证成功!/ 验证失败!”

    namespace _2017_2_17__验证码
    {
        class Program
        {
            static void Main(string[] args)
            {//生成验证码
                String yzm = "";
                String all = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                Random n = new Random();
                for (int i = 0; i < 4;i++ )
                {
                  yzm=yzm+all.Substring(n.Next(0, all.Length), 1);
                }
                Console.Write(yzm);
                //验证
                Console.Write("请输入验证码:");
                String y=Console.ReadLine();
                if (y.Length != 4)
                {
                    Console.Write("您输入的验证码位数不对");
                }
                else 
                {
                    if (yzm.ToLower() == y.ToLower())
                    {
                        Console.Write("验证成功");
                    }
                    else 
                    {
                        Console.Write("验证失败");
                    }
                }
                Console.ReadLine();
            }
        }
    }

    3、“请输入身份证号(18位):”
    判断正确性:
    全数字
    最后一位x/X
    中间时间是否正确

    “您的生日是:2000年1月1日”

    namespace _2017_2_27___判断身份证号格式
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.Write("请输入身份证号:");
                String a=Console.ReadLine();
                int c = a.Length;
                if (c == 18)
                {
                    string wei17 = a.Substring(0, a.Length - 1);
                    try 
                    {
                        Convert.ToInt64(wei17);
                        string last = a.Substring(a.Length-1,1);
                        bool LastIsNumber = true;
                        try { Convert.ToInt32(last); }
                        catch { LastIsNumber = false; }
                        if (last.ToLower() == "x" || LastIsNumber)
                        {
                            String year = a.Substring(6, 4);
                            String month = a.Substring(10,2);
                            String day = a.Substring(12,2);
                            try
                            { DateTime dt = new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(day));
                            Console.Write("您的出生年月是:"+dt.ToString("yyyy年MM月dd日"));
                            }
                            catch 
                            { Console.Write("身份证日期不正确"); }
                        }
                        else 
                        {
                            Console.Write("最后一位的格式不正确");
                        }
                    }
                    catch 
                    {
                        Console.Write("输入的格式不正确");
                    }
    
    
                }
                else 
                {
                    Console.Write("您输入的位数错误"); 
                }
                Console.ReadLine();
            }
        }
    }

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

    判断是否正确

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

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

    namespace _2017_2_27____世界末日
    {
        class Program
        {
            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 d=Convert.ToInt32(ts.Days);
                    if(d<0)
                    {
                        Console.WriteLine("距离世界末日还有"+Math.Abs(d)+"");
                    }
                    else if (d > 0)
                    {
                        Console.WriteLine("超过世界末日" + Math.Abs(d) + "");
                    }
                    else 
                    {
                        Console.WriteLine("今天是世界末日");
                    }
                }
                catch { Console.Write("您输入的日期有误"); }
                Console.ReadLine();
            }
        }
    }
  • 相关阅读:
    Python小白的数学建模 ---- 系列课程
    Maven学习笔记
    JavaScript 中的 Var,Let 和 Const 有什么区别
    (鸡汤文)搞懂了 JavaScript 定时器 setTimeout() 的 this 指向!
    setTimeout返回值的验证,(〒︿〒) 请原谅我一直以来对你的忽视
    终于把初中到大学的数学知识梳理完了(学习算法必备数学知识)
    最简单入门深度学习
    机器学习基本流程
    Vue.js源码解析-Vue初始化流程
    最大公约数&最小公倍数
  • 原文地址:https://www.cnblogs.com/zhengqian/p/6476793.html
Copyright © 2020-2023  润新知