类 string math

//String Math
string a = " abCdefgd8 ";
int c = a.IndexOf("d");//从前面开始找,找到第一个,数它的索引号
int d = a.LastIndexOf("d");
bool b = a.Contains("dd");//是否包含此字符串
Console.WriteLine(d);

int b = a.Length;//长度
Console.WriteLine(b);
string c = a.Trim();//去掉前后空格
Console.Write(c);
Console.Write("\n");
string d = a.TrimStart();//去掉前空格
Console.Write(d);
Console.Write("\n");
string e = a.TrimEnd();//去掉后空格
Console.Write(e);
Console.Write("\n");
string f = a.ToUpper();//全部将小写字母转换为大写
Console.WriteLine(f);
string g = a.ToLower();//全部将大写字母转换为小写
Console.WriteLine(g);

索引号是从0开始的

string h = a.Substring(4);
//里面一个值,表示从这个索引开始一直截取到最后
Console.WriteLine(h);
Console.WriteLine(a.Substring(8));

//a = a.Substring(8);//若果不重新赋值,a是没有变化的

//两个值,表示从哪个索引号开始,截取多少长度
string i = a.Substring(4, 3);
Console.WriteLine(i);


a = a.Replace("de", "DE");

Console.WriteLine(a);


string j = "2012 12 23";
string[] aa = j.Split();//分割字符串,以什么字符
foreach (string m in aa)
{
Console.WriteLine(m);
}

Math类
double a = 1.5;
Console.WriteLine(Math.Ceiling(a));//取上线
Console.WriteLine(Math.Floor(a));//取下线
Console.WriteLine(Math.PI*a);//π 圆周率
Console.WriteLine(Math.Sqrt(a));//开平方根
Console.WriteLine(Math.Round(a));//四舍五入
//注意:奇数.5的情况下,取上线
//偶数.5的情况下,取下线

案例

//输入身份证号,截取生日,输出

for (; ; )
{
Console.Write("请输入身份证号码:");
string a = Console.ReadLine();
if (a.Length == 18)
{
int b = int.Parse(a.Substring(6, 4));
int c = int.Parse(a.Substring(10, 2));
int d = int.Parse(a.Substring(12, 2));
Console.WriteLine(b + "年" + c + "月" + d + "日");
break;
}
else
{
Console.WriteLine("您的输入有误!");
}
}
Console.ReadLine();

案例

 

// 练习:判断邮箱格式是否正确
//1.有且只能有一个@
//2.不能以@开头
//3.@之后至少有一个.
//4.@和.不能靠在一起
//5.不能以.结尾
Console.Write("请输入你的邮箱:");
string a = Console.ReadLine();
if (a.Contains("@"))
{
int c = a.IndexOf("@");
int d = a.LastIndexOf("@");
if (d == c)
{
string e = a.Substring(c);

 

if (e.Contains("."))
{
if (e.IndexOf(".") != 1)
{
if (e.LastIndexOf(".") != e.Length - 1)
{
Console.WriteLine("您输入的邮箱格式正确!");
}
else
{
Console.WriteLine("您的输入有误");
}
}
else
{
Console.WriteLine("您的输入有误");
}

 

}
else
{
Console.WriteLine("您的输入有误");
}
}
else
{
Console.WriteLine("您的输入有误");
}
}
else
{ Console.WriteLine("您的输入有误"); }

 

Console.ReadLine();

 

//随机数类 Random

Random ran = new Random();
int a=ran.Next(10);
Console.WriteLine(a);

案例//随机出验证码,对照输入,判断是否正确,错误重新输入
string s = "abcdefghijklmnopqrstubwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for (; ; )
{
Random ran = new Random();
string y = "";
for (int i = 1; i <= 4; i++)
{
y += s.Substring(ran.Next(s.Length), 1);
}
Console.WriteLine(y);
Console.Write("请输入验证码:");
string y1 = Console.ReadLine();
if (y.ToLower() == y1.ToLower())
{
Console.WriteLine("您的输入正确");
break;
}
else
{
Console.Clear();
Console.WriteLine("您的输入有误,请重新输入:");
}
}

Console.ReadLine();

 

 

转载于:https://www.cnblogs.com/nannan-0305/p/5268457.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值