您的位置:首页 > 其它

类型

2015-11-23 10:06 190 查看
//必须通过new初始化变量,后面括号是构造函数

int cuo = 0;
Console.WriteLine("请输入时间");
string s = Console.ReadLine();
//datetime实际是一个类。
try
{
DateTime dt = DateTime.Parse(s);

}
catch (Exception ex)
{
Console.WriteLine("不是");
cuo = 1;

}

if (cuo == 0)
{
Console.WriteLine("正确   ");
}
//类 类型 class 类型;用户自定义类型
//String :处理字符串
//Datetime:处理时间
//Ramdom: 生成随机数
//Math:处理数字

string s = "      adasdasda";

int changdu = s.Length;//返回字符串的长度
Console.WriteLine(changdu);
s.Trim(); //去除空格。
s.TrimEnd();//去后空格
s.TrimStart();//去前空格
s.ToLower();//转化为小写
s.ToUpper();//抓化为大写

//截取字符串;字符串是有索引的,索引从0开始

string js = "abcdefghabcdefgabcdefgabcdefgefi";
string jqh = js.Substring(2);
Console.WriteLine(jqh);

int id = js.IndexOf("ef",5);//差第一个匹配项的索引
Console.WriteLine(id);

int idd = js.LastIndexOf("ef");
Console.WriteLine(idd);

string news = js.Replace("ef", "123");

Console.WriteLine(js);
Console.WriteLine(news);

string s = "a|b|c|d|ab|cd|";
string[] str = s.Split('|');
foreach (string d in str)
{
Console.WriteLine(d);
}
//Math类:里边有些处理数字的方法,静态方法
int i=  Math.Abs(-5);//取绝对值
Console.WriteLine(i);
double a = Math.Ceiling(1.1);//上限取整数

Console.WriteLine(a);
double b= Math.Floor(1.9);//取下线整数值

Console.WriteLine(b);
Console.WriteLine(Math.Round(1.52));//四舍五入
Console.WriteLine(Math.PI);//圆周率
Console.WriteLine(Math.Pow(2, 3));//指定数字的几次方
Console.WriteLine(Math.Sqrt(16));//求平方根
Console.ReadLine();

//随机数
Random r = new Random();

for (int i = 0; i <= 100; i++)
{
int shu = r.Next(1001, 9999);
Console.WriteLine(shu);
Thread.Sleep(100);
Console.Clear();
}

Console.WriteLine("中奖人是曹凯" );
DateTime dt = new DateTime(2015,11,20,15,32,50);
int year= dt.Year;
int mouth= dt.Month;
Console.WriteLine(year);
Console.WriteLine(mouth);

DateTime dt2 =  DateTime.Now;//获取系统当前时间。
Console.WriteLine(dt2.ToString());
int sumday = dt2.DayOfYear;//获取改年中的第几天
Console.WriteLine(sumday);

DateTime dt3 = dt2.AddYears(3);
Console.WriteLine(dt3.ToString("yyyy年MM月dd日"));

//字符串型转DateTime型

string st = "2015/9/14";
Console.WriteLine(st.ToString());

Console.ReadLine();


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: