String类:
.Length字符的长度
 
.Trim()去掉开头以及结尾的空格
.TrimStart()去掉字符串开头的空格
.TrimEnd()去掉字符串后面的空格
 
.ToUpper()全部大写
.ToLower()全部小写
 
Substring(起始位置,截取长度)
Substring(起始位置)只写起始位置,可以截取到尾
身份证截取生日
 
IndexOf("字符串")返回第一次出现此字符串的索引
LastIndexOf("字符串")返回最后一次出现此字符串的索引
 
StartsWith("字符串")是否以此字符串为开头,返回True或False
EndsWith("字符串")是否以此字符串为结尾
Contains("字符串")是否包含此字符串。返回True或者False
 
Replace("老字","新字")将老字用新字替换
 
 
Math类:
Ceiling()      取上线
Floor()取下线
Math.PI圆周率
Math.Sqrt()平方根
Math.Round()四舍五入(注意奇数偶数下.5不一样的结果)

Random类

初始化      实例化(必须初始化)
不允许将初始化的语句放置在循环中

string xx=“12345678。。。。30”

 Random ran = new Random();

int b=xx.length;

int c= ran.Next(b)  //  输出值为整型(随机长度)

Console.WriteLine(c);

例子:

            string xx = "abcdefghijklmnopqrstuvwsyzABCDEFGHIGKLMNOPQRSTUVWSYZ123456789";
            Random xy = new Random();
            int c = xx.Length;
            int a1 = xy.Next(c);                              //截取点;
            string b1 = xx.Substring(a1, 1);
            int a2 = xy.Next(c);
            string b2 = xx.Substring(a2, 1);
            int a3 = xy.Next(c);
            string b3 = xx.Substring(a3, 1);
            int a4 = xy.Next(c);
            string b4 = xx.Substring(a4, 1);
            string d = (b1 + b2 + b3 + b4);
            Console.Write("输入验证码:");
            string e = Console.ReadLine();
            string f = e.ToUpper();
            if (f == d)
            {
                Console.Write("输入正确!");
            }
            else
            {
                Console.WriteLine("输入有误!");
            }
 
DateTime类:
注意在使用之前需要先初始化一遍。
DateTime dt =new DateTime();
若获取当前时间,可以不用初始化:
DateTime dt =DateTime.Now;//系统当前时间,运行时查询
获取年dt.Year
获取月dt.Month
获取日dt.Day
获取小时dt.Hour
获取分dt.Minute
获取秒dt.Second
            //计算一下北京奥运会已经开了多少天
            DateTime dt1 = DateTime.Parse("2008/8/8 20:00:00");
            DateTime dt2 = DateTime.Now;
            Console.WriteLine(Math.Floor((dt2 - dt1).TotalDays));
 
获取这一天是星期几 
DayOfWeek d = dt.DayOfWeek;
获取到的是英文。
若想用中文,先d.ToString()
然后根据英文打印出中文。
 
yyyy年
MM月
dd日
hh时
mm分
ss秒
以上是代位符。可以在字符串中先占用下这个空位。
string s = dt.ToString("yyyy年MM月dd日hh时mm分ss秒");
 
DateTime可以增加或者减去相应的时间
Add()增加或者减去一定的时间间隔
AddYears()增加或减去年份
AddMonths()增加或减去月份
AddDays()增加或减去天数
以此类推。
注意,加减天数,小时数是利用double类型。其他都是int类型
            异常保护    try  catch    finally
            Console.Write("请输入一个整数:");
            try //尝试去做,若有错,从错误的语句直接跳转到catch
            {
                int a = int.Parse(Console.ReadLine());
                Console.WriteLine("输入正确!!");
            }
            catch//只有在报错的情况下才执行
            {
                Console.WriteLine("输入有误!!");
            }
            Console.WriteLine("感谢您的使用,再见!");
            finally //不管对错,都会进行执行的语句
            {
                Console.WriteLine("感谢您的使用,再见!");
            }
 
 

C#string类;math类;datetime类的更多相关文章

  1. Java常用类:包装类,String,日期类,Math,File,枚举类

    Java常用类:包装类,String,日期类,Math,File,枚举类

  2. 类:String,Math,DateTime,Random

    string类: 判断邮箱格式是否正确: 1.有且只能有一个@  2.不能以@开头  3.@之后至少有一个.  4.@和.不能靠在一起  5.不能以.结尾 math 类: math.ceiling() ...

  3. 类:String,Math,DateTime,Random随机数,异常保护

    String类: 练习: Math类: Random随机数: DateTime类: 异常保护: 练习: 1. 2. 3.方法一: 方法二: 4.人机大战石头剪刀布 5. //请输入你想输入的数字 // ...

  4. string、math、random、datetime类

    1.string类 变量.Replace("想要替换掉的字符或字符串","转换后的字符或字符串");//替换 练习:判断邮箱格式是否正确            ...

  5. 类之string类、Math类、DateTime类

    String类 string a = "abcdef123456"; 注:字符串的长度是从0开始计数的如:0,1,2,3,4,5,6,7,8,9........ a.Length; ...

  6. 【2017-02-26】String类、Math类、DateTime类

    一.String类 黑色小扳手 - 属性     后面不带括号紫色立方体 - 方法     后面带括号 字符串.Length  -  字符串长度,返回int类型 字符串.TrimStart() - 去 ...

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

    一string类 (1)字符串.Length    Length作用于求字符串的长度,返回一个int值 (2)字符串.TrimStart();  TrimStart():可删除前空格,返回一个stri ...

  8. 【2017-2-26】C#String类、Math类、DateTime类

    String类 黑色小扳手:属性      后面不带括号 紫色小箱子:方法      后面带小括号 1.字符串.Length;   字符串长度,返回int类型 字符串的长度 2.字符串.TrimSta ...

  9. C#基础 类及常用函数【string 、Math 、DiteTime 、TimeSpan】

    一  string 类型 string str = "abcdefg"; str.Length  -  字符串长度,返回int类型 str.TrimStart()          ...

随机推荐

  1. POJ 2031 Building a Space Station

    3维空间中的最小生成树....好久没碰关于图的东西了.....              Building a Space Station Time Limit: 1000MS   Memory Li ...

  2. PHP如何释放内存之unset销毁变量并释放内存详解

    PHP的unset()函数用来清除.销毁变量,不用的变量,我们可以用unset()将它销毁.但是某些时候,用unset()却无法达到销毁变量占用的内存!我们先看一个例子: <?php $s = ...

  3. CentOS-6.5-saltstack-安装

    官方网站:https://www.saltstack.com/ 官方文档   https://docs.saltstack.cn/contents.html GitHub:  https://gith ...

  4. Redis学习笔记十:独立功能之监视器

    通过执行 monitor 命令可以让客户端自己变成一个监视器,实时接收并打印当前处理的命令请求的相关信息. 127.0.0.1:6379> monitor OK 1451752646.83727 ...

  5. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  6. window.location.href 和 window.location.replace 的区别

    window.location.href  和  window.location.replace 的区别 1.window.location.href=“url”:改变url地址: 2.window. ...

  7. windows和linux文件共享

    ###Samba安装     [root@samba ~]# yum install -y samba*     [root@samba ~]# rpm -qa | grep samba ###开启s ...

  8. Windows上安装使用MongoDB(一)

    首先下载MongoDB的Windows版本,从如下地址: https://www.mongodb.org/downloads. 我下载的msi版本,下载后安装即可,如我安装的盘符是:C:\Progra ...

  9. 修复 VirtualBox 下 Ubuntu 14.10 屏幕分辨率问题

    在 Windows 7 下使用 VirtualBox 安装了一个 Ubuntu 14.10 后,碰到了一个 640×480 屏幕分辨率的问题. 在 ‘Display Settings' 设置界面的 ‘ ...

  10. Sqli-LABS通关笔录-12

    通过这个关卡我学习到了: 1.双引号千万别忘记,就是因为忘了弄了好一会儿.一直不报错. 2. 00x1万能密码构造二 报错的内容为: You have an error in your SQL syn ...