一、string类:
.Length 字符串的长度

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

.ToLower() 全部转换为小写
.ToUpper() 全部转换为大写

.Substring(m,n) 从m位开始截取n长度的字符串
.Substring(m) 从m位开始截取至字符串结尾

.IndexOf("字符串") 返回第一次出现该字符串的索引
.LastIndexOf("字符串") 返回最后一次出现该字符串的索引

.StartsWith("字符串") 是否以该字符串开头,返回ture或false
.EndsWith("字符串") 是否以该字符串结束,返回ture或false
.Contains("字符串") 是否包含此字符串,返回ture或false

.Replace("新词","老词") 用新词替换老词

二、Math类
Ceiling() 取上线
Floor() 取下线
Math.PI 圆周率
Math.Sqrt() 平方根
Math.Round() 四舍五入(奇数.5-上位,偶数.5-舍去)
Math.Abs() 绝对值
Math.Pow(m,n) 返回m的n次方
Math.Max(m,n) m与n两者取大

三、Random类(随机数类)
Random a = new Random();//初始化,实例化(不可以放在循环当中)
Console.WriteLine(a.Next());//Next()非负随机数,1.m>=0 2.0<=m<x 3.x<=m<y

四、DateTime类
注意:在使用前需要先初始化一遍
Datetime dt = new Datetime();
若获取当前时间可以不用初始化

Datetime dt = Datetime.Now;//获取当前时间,运行词语时的本地时间
dt.Year 获取年份
dt.Month 获取月份
dt.Day 获取日
dt.Hour 获取小时
dt.Minute 获取分
dt.Second 获取秒

DayOfWeek d = dt.DayOfWeek;//获取日期为星期几
DayOfYear tian = dt.DayOfYear;//返回今年的第n天

string s = dt.ToString("yyyy年MM月dd日hh时mm分ss秒");//用字符串的形式显示时间。

yyyy,MM,dd,hh,mm,ss为占位符

DateTime可以增加或减去相应的时间
string s = Console.ReadLine();
DateTime dt = new DateTime();
dt = DateTime.Parse(s);
System.TimeSpan time = new TimeSpan();//TimeSpan是一个时间间隔类型
dt = dt.Add(time);//参数是TimeSpan类型
dt = dt.AddDays(1.5);//加1.5天
dt = dt.AddYears(2);//加2年

练习:
1、//生成一个由大小写及数字组成的验证码
//随机生成一个四个字符长度的验证码,输出一下
//让用户对比输入
//正确输出【输入正确】不正确输出【输入有误】

string zu = "ABCDEFGHIJKLMNOPQRST0123456789";//定义一个包含所有选项的字符串
int changdu = zu.Length;//计算这个字符串的长度
Random ran = new Random();//定义一个随机数ran
int x = ran.Next(changdu);//随机数ran的范围是chagndu,从中随机抽取一个
string a = zu.Substring(x, 1);//从zu字符串中截取一个字符,这个字符是从x位开始长度为1的字


int y = ran.Next(changdu);
string b = zu.Substring(y, 1);
int z = ran.Next(changdu);
string c = zu.Substring(z, 1);
int o = ran.Next(changdu);
string d = zu.Substring(o, 1);
string yzm = a + b + c + d;
Console.WriteLine("验证码为:" + yzm);
Console.Write("请输入你所获取的验证码:");
string sryzm = Console.ReadLine().ToUpper();
if (sryzm == yzm)
{
Console.WriteLine("验证码输入正确,正在登入中...");
}
else
{
Console.WriteLine("验证码输入有误。");
}

2、//玩家与计算机玩剪刀石头布的游戏
//剪刀-1 石头-2 布-3
for (; ; )
{
Console.Write("剪刀石头布,你要出哪个?");
string x = Console.ReadLine();
int xx = 0;
if (x == "剪刀" || x == "石头" || x == "布")
{
if (x == "剪刀")
{
xx = 1;
}
else if (x == "石头")
{
xx = 2;
}
else if (x == "布")
{
xx = 3;
}
Random y = new Random();
int yy = y.Next(1, 4);
string z = "1";
if (yy == 1)
{
z = "剪刀";
}
if (yy == 2)
{
z = "石头";
}
if (yy == 1)
{
z = "布";
}
Console.WriteLine("你出的是" + x);
Console.WriteLine("计算机出的是" + z);
if (xx - yy == 0)
{
Console.WriteLine("我们是平手。");
}
else if (xx - yy == 1 || xx - yy == -2)
{
Console.WriteLine("恭喜你获得胜利!");
}
else
{
Console.WriteLine("很遗憾计算机获得胜利!");
}
}
else
{
Console.WriteLine("你的输入有误");
}
Console.WriteLine();
}

3///输入一个四位数的金额,将其转换为汉字大写
Console.Write("请输入一个四位数的金额:");
int j = int.Parse(Console.ReadLine());
if (j >= 1000 && j <= 9999)
{
string je = j.ToString();
string dx = "仟佰拾元零壹贰叁肆伍陆柒捌玖";
string s2 = "", s3 = "", s4 = "";
s2 = je.Substring(1, 1);
s3 = je.Substring(2, 1);
s4 = je.Substring(3, 1);
string m = "";
string n = "";
if (s2 == "0" && s3 == "0" && s4 == "0")
{
int a = int.Parse(je.Substring(0, 1));
m = dx.Substring(a + 4, 1);
n = dx.Substring(0, 1);
Console.WriteLine("您输入的金额为:" + m + n + "元。");
}
else if (s2 == "0" && s3 == "0" && s4 != "0")
{
int a = int.Parse(je.Substring(0, 1));
m = dx.Substring(a+4,1);
n = dx.Substring(0, 1);
int b = int.Parse(je.Substring(3, 1));
string c = dx.Substring(b+4,1);
Console.WriteLine("您输入的金额为:" + m + n + "零" + c + "元。");
}
else if (s2 != "0" && s3 == "0" && s4 == "0")
{
Console.Write("您输入的金额是:");
for (int i = 0; i < 2; i++)
{
int a = int.Parse(je.Substring(i, 1));
m = dx.Substring(a + 4, 1);
n = dx.Substring(i, 1);
Console.Write(m + n);
}
Console.WriteLine("元。");
}
else
{
Console.Write("您输入的金额是:");
for (int i = 0; i < 4; i++)
{
int a = int.Parse(je.Substring(i, 1));
m = dx.Substring(a + 4, 1);
if (a != 0)
{
if (i != 3)
{
n = dx.Substring(i, 1);
}
else
{

n = "";
}
}
else
{
if (i == 3)
{
m = "";
}
n = "";
}
Console.Write(m + n);
}
Console.Write("元。");
Console.WriteLine();
}
}
else
{
Console.WriteLine("您输入的金额有误。");
}

C#-类-string/Manth/Random/DateTime-及练习的更多相关文章

  1. 类-string/Manth/Random/DateTime-及练习

    类一.string类:.Length 字符串的长度 .Trim() 去掉开头以及结尾的空格.TrimStart() 去掉开头的空格.TrimEnd() 去掉结尾的空格 .ToLower() 全部转换为 ...

  2. python----常用模块(random,string,time&datetime,os,sys,xpinyin(拼音))

    一.模块.包 1.1 什么是模块  在python中,一个.py文件就构成一个模块,意思就是说把python代码写到里面,文件名就是模块的名称,test.py test就是模块名称. 1.2 什么是包 ...

  3. 高并发分布式系统中生成全局唯一(订单号)Id js返回上一页并刷新、返回上一页、自动刷新页面 父页面操作嵌套iframe子页面的HTML标签元素 .net判断System.Data.DataRow中是否包含某列 .Net使用system.Security.Cryptography.RNGCryptoServiceProvider类与System.Random类生成随机数

    高并发分布式系统中生成全局唯一(订单号)Id   1.GUID数据因毫无规律可言造成索引效率低下,影响了系统的性能,那么通过组合的方式,保留GUID的10个字节,用另6个字节表示GUID生成的时间(D ...

  4. C# 字符串string类型转换成DateTime类型 或者 string转换成DateTime?(字符串转换成可空日期类型)

    在c#中,string类型转换成DateTime类型是经常用到的,作为基本的知识,这里在此做个小结.一般来说可以使用多种方法进行转换,最常用的就是使用Convert.ToDateTime(string ...

  5. 字符串string类型转换成DateTime或DateTime?类型

    常用的Convert.ToDateTime方法 //将含有正确日期格式的string类型转换成DateTime类型 string strDate = "2014-08-01"; D ...

  6. C#部分---arraylist集合、arraylist集合中的object数据转换成int类string类等;间隔时间的表示方法;

    ArrayList和Array的区别: 相同点:1.两者都实现了IList.ICollection.IEnumerable接口:       2.两者都可以使用证书索引访问集合中的元素,包括读取和赋值 ...

  7. JAVA基础--常用类 String,StringBuffer, 基础数据类型包装类, Math类, Enum类

    字符串相关类: String, StringBuffer String类为不可变的字符序列 String s1="hello"; String s2="hello&quo ...

  8. Random类和Math.random()方法

    一.Random类的定义Random类位于 java.util 包中,主要用于生成伪 随机数Random类将 种子数 作为随机算法的起源数字,计算生成伪随机数,其与生成的随机数字的区间无关创建Rand ...

  9. 编写类String的构造函数、析构函数和赋值函数

    已知类String的原型为: class String {   public:  String(const char *str = NULL); // 普通构造函数  String(const Str ...

随机推荐

  1. UI7Kit

    [UI7Kit] UI7Kit is a GUI toolkit which can backport flat-style UIKit from iOS7 to iOS5/iOS6. Additio ...

  2. python获取参数

    argparse是python的一个命令行参数模块,可以解析命令行参数,生成帮助. 示例: #!/usr/bin/python from argparse import ArgumentParser ...

  3. (一)JQuery动态加载js的三种方法

    Jquery动态加载js的三种方法如下: 第一种: $.getscript("test.js"); 例如: <script type="text/javascrip ...

  4. 解决idea gradle构建Received fatal alert: handshake_failure问题

    Gradle是一款强大的构建工具,但是搭建项目运行环境总是非常头痛,各种网络原因会导致项目不能成功的导入. 说一下这个问题的解决办法,折腾了很久终于解决了. javax.net.ssl.SSLHand ...

  5. linux常见命令-查看磁盘空间

    linux查看磁盘使用情况命令 1. 统一每个目录下磁盘的整体情况: df -h 2. 查看指定目录,在命令后直接放目录名,比如查看“usr”目录使用情况:df -h  /usr/ 3. 查看当前目录 ...

  6. Windbg and resources leaks in .NET applications 资源汇总

    Windows Forms Leaks 1.http://blogs.msdn.com/b/tess/archive/2008/02/04/net-debugging-demos-informatio ...

  7. ASP.NET系列:自定义配置节点的复用

    appSettings太简单,为每个程序自定义配置节点太复杂,因此要解决app.config&web.config自定义配置的复用问题. 1.读取不依赖SectionName,根节点可以定义为 ...

  8. 关于 Kafka offset

    查询topic的offset的范围 用下面命令可以查询到topic:Mytopic broker:SparkMaster:9092的offset的最小值: bin/kafka-run-class.sh ...

  9. 搬家至独立博客 https://www.imzjy.com/blog/

    欢迎访问 https://www.imzjy.com/blog/

  10. 基于python 3.5 所做的找出来一个字符串中最长不重复子串算法

    功能:找出来一个字符串中最长不重复子串 def find_longest_no_repeat_substr(one_str): #定义一个列表用于存储非重复字符子串 res_list=[] #获得字符 ...