7.25

//String类
string s = " abCDefgb ";
//int a = s.Length;//获取长度
Console.WriteLine(s.Length);

//去掉前后空格
Console.Write(s.Trim());
//只去掉前面的空格
Console.Write(s.TrimStart());
Console.WriteLine(123);
//只去掉后面的空格
Console.Write(s.TrimEnd());

//将全部小写字母转换为大写
Console.WriteLine(s.ToUpper());
//将所有大写字母转换为小写
Console.WriteLine(s.ToLower());

//返回第一次出现该字符或字符串的索引号
//注意:索引号是从0开始
//返回值为-1.表示没有找到该字符或字符串
Console.WriteLine(s.IndexOf("abc"));
//返回最后一次出现该字符或字符串的索引号
Console.WriteLine(s.LastIndexOf("b"));

//substring截取字符串
//写一个参数的时候,表示从这个索引号开始截取,一直到最后
Console.WriteLine(s.Substring(3));
//两个参数表示,从哪个位置开始截取,截取多长
Console.WriteLine(s.Substring(4, 4));

//startswith 是否以**字符串开头
Console.WriteLine(s.StartsWith("ab"));
//endswith 是否以**字符串结尾
Console.WriteLine(s.EndsWith("b"));

//contains 是否包含
Console.WriteLine(s.Contains("CD"));

//replace 替换
Console.WriteLine(s.Replace("b", "BB"));

Console.WriteLine(s);
Console.ReadLine();


请输入您的身份证号,为您截取出来您的生日
370321199003053330
Console.Write("请输入您的身份证号:");
string cid = Console.ReadLine();
string year = cid.Substring(6, 4);
string month = cid.Substring(10, 2);
string day = cid.Substring(12, 2);
Console.WriteLine("您的出生日期为:{0}年{1}月{2}日。", year, month, day);
Console.ReadLine();

//练习:判断邮箱格式是否正确
//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("."))
{
int c = mail1.IndexOf(".");
if (c != 1)
{
if (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("您的邮箱格式不正确!");
}
}
else
{
Console.WriteLine("您的邮箱格式不正确!");


Console.ReadLine();


//输入两个时间日期,计算出相差多少天(TotalDays)
//Console.Write("请输入第一个时间日期(****/**/** **:**:**):");
//DateTime dt1 = DateTime.Parse(Console.ReadLine());
//Console.Write("请输入第二个时间日期(****/**/** **:**:**):");
//DateTime dt2 = DateTime.Parse(Console.ReadLine());

//Console.WriteLine((dt2-dt1).TotalDays);


//Math类 数学类
//ceiling 天花板 取上线
Console.WriteLine(Math.Ceiling(4.4));
//floor 地板 取下线
Console.WriteLine(Math.Floor(4.4));
//sqrt 开平方根
Console.WriteLine(Math.Sqrt(4));
//pi π 3.141592
Console.WriteLine(Math.PI);
//round 四舍五入
//奇数.5的时候取得是上线
//偶数.5的时候取得是下线
Console.WriteLine(Math.Round(4.5));
Console.ReadLine();


DateTime 时间日期类型
使用之前应该进行初始化
DateTime dt = new DateTime();
获取当前时间
DateTime dt = DateTime.Now;
Console.WriteLine(dt);

Console.WriteLine(dt.Month);
获取年 dt.Year
获取月 dt.Month
获取日 dt.Day
获取小时 dt.Hour
获取分 dt.Minute
获取秒 dt.Second


获取星期几
DayOfWeek d = dt.DayOfWeek;
//Console.WriteLine(d);
string dow =d.ToString();
switch(dow)
{
case "Monday":
Console.WriteLine("星期一");
break;
case "Tuesday":
Console.WriteLine("星期二");
break;
}

原文地址:https://www.cnblogs.com/power8023/p/5712690.html