日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#基础 类

發布時間:2025/3/17 C# 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的C#基础 类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。