C#基础 类
類:系統內置的處理字符串類型的函數方法類。
string是String的快捷方式。所包含的內容都是一樣的。
Int i=x.length;//獲取一個字符串長度
字符串中,索引號從0開始
String類
?
?字符串當中,索引號是從0開始的
去掉字符串前后空格
Console.Write(a.Trim());
去掉前面的空格
Console.Write(a.TrimStart());
去掉后面的空格
Console.Write(a.TrimEnd());
將所有大寫轉換為小寫
Console.WriteLine(a.ToLower());
將所有小寫字母轉換為大寫
Console.WriteLine(a.ToUpper());
截取字符串,從開始索引截取到最后
Console.WriteLine(a.Substring(5));
截取字符串,從開始索引截取指定的長度
Console.WriteLine(a.Substring(5, 2));
替換
Console.WriteLine(a.Replace("l", "a"));
查找開頭第一次出現的索引號,返回值為-1.表示沒有找到
Console.WriteLine(a.IndexOf("l"));
從后向前找
Console.WriteLine(a.LastIndexOf("l"));
查看是否以空格開頭
Console.WriteLine(a.StartsWith(" "));
查看是否以o結尾
Console.WriteLine(a.EndsWith("o"));
查看是否包含a
Console.WriteLine(a.Contains("a"));
???
數學類 ???Math
取上線
double a = 3.14;
Console.WriteLine(Math.Ceiling(a));
取下線
Console.WriteLine(Math.Floor(a));
開平方根
Console.WriteLine(Math.Sqrt(a));
圓周率
Console.WriteLine(Math.PI);
四舍五入
Console.WriteLine(Math.Round(1.5));
Console.WriteLine(Math.Round(2.5));
奇數.5的情況下取上線
偶數.5的情況下取下線
Console.ReadLine();
隨機數類 ??Random
需要使用隨機數的時候需要先初始化
Random ran = new Random();
int a = ran.Next(10);
Console.WriteLine(a);
// 練習:判斷郵箱格式是否正確
//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("."))
{
//731944381@qq.com
if (mail1.IndexOf(".") != 1&&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("格式錯誤!");
}
Console.ReadLine();
?
?
?
驗證碼:隨機出四位驗證碼
A~Z a~z 0~9
不區分大小寫
string ss = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghjklmnopqrstuvwxyz0123456789";
Random ran = new Random();//隨機數類的初始化
//int a = ran.Next(62);
//int b = ran.Next(62);
//int c = ran.Next(62);
//int d = ran.Next(62);
//string aa = ss.Substring(a, 1);
//string bb = ss.Substring(b, 1);
//string cc = ss.Substring(c, 1);
//string dd = ss.Substring(d, 1);
//string rect = aa + bb + cc + dd;
string rect = "";
for (int i = 0; i < 4; i++)
{
int a = ran.Next(62);
rect += ss.Substring(a,1);
}
Console.WriteLine("驗證碼是:" + rect);
Console.Write("請對照輸入驗證碼:");
string shu = Console.ReadLine();
if (shu.ToUpper() == rect.ToUpper())
{
Console.WriteLine("輸入正確!");
}
else
{
Console.WriteLine("輸入錯誤!");
}
Console.ReadLine();
?
?
輸入您的身份證號,打印出您的生日
Console.Write("輸入您的身份證號");
string a = Console.ReadLine();
string c = a.Substring(6,4);
string d = a.Substring(10, 2);
string e = a.Substring(12, 2);
Console.WriteLine("您的生日"+c+"年"+d+"月"+e+"日");
Console.ReadLine();
?
Datetime類 日期時間
若需要使用,首先需要初始化
DateTime dt = new DateTime();
Console.Write(" 請輸入一個日期時間:****/**/** **:**:**");
dt = DateTime.Parse( Console.ReadLine());
若直接獲取當前時間,不用進行初始化
DateTime dt1 = DateTime.Now;
//Console.WriteLine(dt);
Console.WriteLine(dt1);
//在dt1身上增加10天
Console.WriteLine(dt1.AddDays(10));
//增加10個小時
Console.WriteLine(dt1.AddHours(10));
//創建時間間隔
TimeSpan time = new TimeSpan(10,10,10,10);
Console.WriteLine(dt1.Add(time));
獲取年 dt.Year
獲取月 dt.Month
獲取日 dt.Day
獲取小時 dt.Hour
獲取分 dt.Minute
獲取秒 dt.Second
Console.WriteLine(dt1.Hour);
DayOfWeek dw = dt1.DayOfWeek;
switch (dw.ToString())
{
case "Monday":
Console.WriteLine("星期一");
break;
}
?
輸入兩個時間日期,計算出相差多少天(TotalDays)
Console.Write("請輸入你們戀愛的時間:");
DateTime dt = DateTime.Parse(Console.ReadLine());
DateTime dt1 = DateTime.Now;
Console.WriteLine((dt1-dt).TotalDays);
?
try catch
異常保護語句
Console.Write("請輸入一個整數:");
try//嘗試
{
int a = int.Parse(Console.ReadLine());
Console.WriteLine(a);
}
catch//若try里面的語句有問題,直接跳到catch執行
{
Console.WriteLine("程序出現錯誤!");
}
//finally//不管對與錯,都要執行
//{
// Console.WriteLine("感謝您的使用!");
//}
Console.WriteLine("感謝您的使用!");
Console.Write("請輸入日期時間:");
try
{
DateTime dt = DateTime.Parse(Console.ReadLine());
Console.WriteLine("您輸入的日期時間格式正確!");
}
catch {
Console.WriteLine("您輸入的日期時間有誤!");
}
Console.WriteLine("感謝您的使用!再見!");
Console.ReadLine();
轉載于:https://www.cnblogs.com/dreamer666/p/5618138.html
總結
- 上一篇: 职场必须要会的餐桌礼仪
- 下一篇: linux服务器重启init 6和reb