Math類
/*
* Math類主要用于一些與數(shù)學(xué)相關(guān)的計算,并提供了很多靜態(tài)方法方便訪問,
* 常用的方法:
* Abs:取絕對值
* Ceiling:返回大于或等于指定的數(shù)字的最小整數(shù)值
* Floor:返回小于等于指定數(shù)字的最大整數(shù)
* Round:返回四舍五入
* Max:返回兩個數(shù)中較大數(shù)的值
* Min:返回兩個數(shù)中較小的值
* Equals:返回指定的對象實例是否相等
*
*/
class Program
{
static void Main(string[] args)
{
//圓周率
Console.WriteLine(Math.PI);
//取絕對值
Console.WriteLine(Math.Abs(-2));
Console.WriteLine("---------------------------------");
//返回大于或等于的最小整數(shù)(小數(shù)點后面有值的就直接+1)
Console.WriteLine(Math.Ceiling(33.0));
Console.WriteLine(Math.Ceiling(33.1));
Console.WriteLine(Math.Ceiling(33.9));
Console.WriteLine("---------------------------------");
//返回小于等于指定數(shù)字的最大整數(shù)(不需要管小數(shù)點后面的值河泳,取整數(shù))
Console.WriteLine(Math.Floor(33.0));
Console.WriteLine(Math.Floor(33.1));
Console.WriteLine(Math.Floor(33.9));
Console.WriteLine("---------------------------------");
//四舍五入
Console.WriteLine(Math.Round(33.0));
Console.WriteLine(Math.Round(33.1));
Console.WriteLine(Math.Round(33.9));
Console.WriteLine(Math.Round(33.232,2));//后面的數(shù)字躯概,代表保留小數(shù)點后幾位
Console.WriteLine(Math.Round(33.236,2));
Console.WriteLine("---------------------------------");
Console.WriteLine(Math.Equals(11, 12));
Console.WriteLine(Math.Equals(11, 11));
Console.WriteLine("---------------------------------");
Console.WriteLine("請輸入第一個數(shù):");
int num1 = int.Parse(Console.ReadLine());
Console.WriteLine("請輸入第二個數(shù):");
int num2 = int.Parse(Console.ReadLine());
Console.WriteLine("兩個數(shù)中較大的數(shù)為{0}", Math.Max(num1, num2));
Console.WriteLine("兩個數(shù)中較小的數(shù)為{0}", Math.Min(num1, num2));
Console.ReadKey();
}
}
Random類
/*
* Next() 每次產(chǎn)生一個不同的隨機正整數(shù)
* Next(int max Value) 產(chǎn)生一個比max Value小的正整數(shù)
* Next(int min Value,int max Value) 產(chǎn)生一個minValue~maxValue的正整數(shù),但不包括maxValue
* NextDouble() 產(chǎn)生一個0.0~1.0的浮點數(shù)
* NextBytes(byte[] buffer) 用隨機數(shù)填充字節(jié)數(shù)的數(shù)組
*
*
*
*/
class Program
{
static void Main(string[] args)
{
//Random隨機數(shù)類
Random rd = new Random();
Console.WriteLine("產(chǎn)生一個隨機的整數(shù):{0}", rd.Next());
Console.WriteLine("產(chǎn)生一個比30小的正整數(shù)數(shù):{0}", rd.Next(30));
Console.WriteLine("產(chǎn)生一個10以內(nèi)的數(shù):{0}",rd.Next(0,11));
Console.WriteLine("產(chǎn)生一個0到1之間的浮點數(shù):{0}", rd.NextDouble());
byte[] b = new byte[5];//byte:0-255 sbyte:-128~127
rd.NextBytes(b);
Console.WriteLine("產(chǎn)生的byte類型的值為:");
foreach(byte i in b)
{
Console.Write(i + " ");
}
Console.ReadKey();
}
}
DataTime結(jié)構(gòu)
/*
* C# DataTime結(jié)構(gòu)用于表示時間纺腊,所表示的范圍是從0001年1月1日點到9999年12月31日24點
* 在DataTime結(jié)構(gòu)中提供了靜態(tài)屬性Now,用于獲取當(dāng)前的日期和事件畔咧,如下
* DateTime.Now
*
* 常用的屬性和方法用于獲取或設(shè)置日期和時間
* Date:獲取實例的日期部分
* Day:獲取該實例所表示的日期是一個月的第幾天
* DayOfWeek:獲取該實例所表示的日期是一周的星期幾
* DayOfYear:獲取該實例所表示的日期是一年的第幾天
* Add(Timespan value) 在指定的日期實例上添加時間間隔值 value
* AddDays(double value) 在指定的日期實例上添加指定天數(shù) value
* AddHours(double value) 在指定的日期實例上添加指定的小時數(shù) value
* AddMinutes(double value) 在指定的日期實例上添加指定的分鐘數(shù) value
* AddSeconds(double value) 在指定的日期實例上添加指定的秒數(shù) value
* AddMonths(int value) 在指定的日期實例上添加指定的月份 value
* AddYears (int value) 在指定的日期實例上添加指定的年份 value
*
* 在使用 DateTime 類中的 Add 方法時需要使用時間間隔類 TimeSpan,該類允許表示的時間間隔范圍是 0 到 64 位整數(shù)。
*
*
*/
class Program
{
static void Main(string[] args)
{
//【實例】使用 DateTime 類獲取當(dāng)前時間揖膜,分別輸出該日是當(dāng)月的第幾天誓沸、星期幾以 及一年中的第幾天,并計算 30 天后的日期
DateTime dt = DateTime.Now;
Console.WriteLine("當(dāng)前日期為:{0}", dt);
Console.WriteLine("當(dāng)前是本月的第{0}天", dt.Day);
Console.WriteLine("當(dāng)前是{0}", dt.DayOfWeek);
Console.WriteLine("當(dāng)前是本年度第{0}天", dt.DayOfYear);
Console.WriteLine("30天后的日期是{0}", dt.AddDays(30));
Console.WriteLine("----------------------------------------------");
//兩個日期的差可由時間間隔類 TimeSpan 的對象來存放壹粟。假設(shè)計算現(xiàn)在距離2021年5月1日勞動節(jié)的天數(shù)
DateTime dt2 = new DateTime(2021,5,1);
TimeSpan ts = dt2 - dt;
Console.WriteLine("間隔的天數(shù)為{0}天", ts.Days);
Console.ReadKey();
}
}
String類
/*
*
* @ 符號:1.取消他在字符中的轉(zhuǎn)義作用 2.將字符串按照原格式輸出
*
*
* 1)字符串的不可變性
* 當(dāng)你給一個字符串重新賦值之后拜隧,老值并沒有銷毀,而是重新開辟了一塊空間存儲新值
*
* 當(dāng)程序結(jié)束后趁仙,GC掃描整個內(nèi)存洪添,如果發(fā)現(xiàn)有的空間沒有被指向,則立即把它銷毀
*
* 2)我們可以將字符串看做是char類型的一個只讀數(shù)組雀费。
* ToCharArray();將字符串轉(zhuǎn)換為char數(shù)組
* new string(char[] chs):能夠?qū)har數(shù)組轉(zhuǎn)換了字符串
*
*
* 4干奢、字符串提供的各種方法
1)、Length:獲得當(dāng)前字符串中字符的個數(shù)
2)盏袄、ToUpper():將字符轉(zhuǎn)換成大寫形式
3)律胀、ToLower():將字符串轉(zhuǎn)換成小寫形式
4)、Equals(lessonTwo,StringComparison.OrdinalIgnoreCase):比較兩個字符串貌矿,可以忽略大小寫
5)炭菌、Split():分割字符串,返回字符串類型的數(shù)組逛漫。
6)黑低、Substring():截取字符串。在截取的時候包含要截取的那個位置。
7)克握、IndexOf():判斷某個字符串在字符串中第一次出現(xiàn)的位置蕾管,如果沒有返回-1、值類型和引用類型在內(nèi)存上存儲的地方不一樣菩暗。
8)掰曾、LastIndexOf():判斷某個字符串在字符串中最后一次出現(xiàn)的位置,如果沒有同樣返回-1
9)停团、StartsWith():判斷以....開始
10)旷坦、EndsWith():判斷以...結(jié)束
11)、Replace():將字符串中某個字符串替換成一個新的字符串
12)佑稠、Contains():判斷某個字符串是否包含指定的字符串
13)秒梅、Trim():去掉字符串中前后的空格
14)、TrimEnd():去掉字符串中結(jié)尾的空格
15)舌胶、TrimStart():去掉字符串中前面的空格
16)捆蜀、string.IsNullOrEmpty():判斷一個字符串是否為空或者為null
17)、string.Join():將數(shù)組按照指定的字符串連接幔嫂,返回一個字符串辆它。
18)、string.Format():格式化字符串
*
*/
class Program
{
static void Main(string[] args)
{
//@ 符號:取消他在字符中的轉(zhuǎn)義作用
string rou = @"E:\net\startstudy";
Console.WriteLine(rou);
//將字符串按照原格式輸出
string words = @"今天天氣好晴朗履恩,
處處好風(fēng)光";
Console.WriteLine(words);
//string s1 = "張三";
//string s2 = "張三";
//既然可以將string看做char類型的只讀數(shù)組娩井,所以我們可以通過下標(biāo)去訪問字符串中的某個元素
string s = "abcdefg";
// s[0] = 'b'; 不能這么做,因為是只讀的
//首先將字符串轉(zhuǎn)換為char類型的數(shù)組
char[] chs = s.ToCharArray();
chs[0] = 'b';
//將字符數(shù)組轉(zhuǎn)換為我們的字符串
s = new string(chs);
Console.WriteLine(s[0]);
Console.WriteLine(s);
//練習(xí)一:隨機輸入你心中想到的一個名字似袁,然后輸出它的字符串長度 Length:可以得字符串的長度
//Console.WriteLine("請輸入你心中想的那個人的名字");
//string name = Console.ReadLine();
//Console.WriteLine("你心中想的人的名字的長度是{0}",name.Length);
//Console.ReadKey();
//Console.WriteLine("請輸入你喜歡的課程");
//string lessonOne = Console.ReadLine();
////將字符串轉(zhuǎn)換成大寫
//// lessonOne = lessonOne.ToUpper();
////將字符串轉(zhuǎn)換成小寫形式
//// lessonOne = lessonOne.ToLower();
//Console.WriteLine("請輸入你喜歡的課程");
//string lessonTwo = Console.ReadLine();
//// lessonTwo = lessonTwo.ToUpper();
//// lessonTwo = lessonTwo.ToLower();
//if (lessonOne.Equals(lessonTwo,StringComparison.OrdinalIgnoreCase))
//{
// Console.WriteLine("你們倆喜歡的課程相同");
//}
//else
//{
// Console.WriteLine("你們倆喜歡的課程不同");
//}
//Console.ReadKey();
//string s = "a b dfd _ + = ,,, fdf ";
////分割字符串Split
//char[] chs = { ' ', '_', '+', '=', ',' };
//string[] str = s.Split(chs,StringSplitOptions.RemoveEmptyEntries);
//Console.ReadKey();
//練習(xí):從日期字符串("2008-08-08")中分割出年洞辣、月、日昙衅;2008年08月08日扬霜。
//讓用戶輸入一個日期格式如:2008-01-02,你輸出你輸入的日期為2008年1月2日
//string s = "2008-08-08";
//// char[] chs = { '-' };
//string[] date = s.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
//Console.WriteLine("{0}年{1}月{2}日", date[0], date[1], date[2]);
//Console.ReadKey();
//老趙
//string str = "你真的太牛逼了";
//if (str.Contains("牛逼"))
//{
// str = str.Replace("牛逼", "**");
//}
//Console.WriteLine(str);
//Console.ReadKey();
//Substring 截取字符串
//string str = "今天天氣好晴朗,處處好風(fēng)光";
//str = str.Substring(1,2);
//Console.WriteLine(str);
//Console.ReadKey();
//string str = "今天天氣好晴朗而涉,處處好風(fēng)光";
//if (str.EndsWith("風(fēng)"))
//{
// Console.WriteLine("是的");
//}
//else
//{
// Console.WriteLine("不是的");
//}
//Console.ReadKey();
//string str = "今天天天氣好晴朗著瓶,天天處天好風(fēng)光";
//int index = str.IndexOf('哈',2);
//Console.WriteLine(index);
//Console.ReadKey();
//string str = "今天天氣好晴朗,處處好風(fēng)光";
//int index = str.LastIndexOf('天');
//Console.WriteLine(index);
//Console.ReadKey();
////LastIndexOf Substring
string path = @"c:\a\b\c\d\e\f\g\\fd\fd\fdf\d\vfd\加油.wav";
int index = path.LastIndexOf("\\");
path = path.Substring(index + 1);
Console.WriteLine(path);
//Console.ReadKey();
// string str = " hahahah ";
//// str = str.Trim();
// //str = str.TrimStart();
// str = str.TrimEnd();
// Console.Write(str);
// Console.ReadKey();
//string str = "fdsfdsfds";
//if (string.IsNullOrEmpty(str))
//{
// Console.WriteLine("是的");
//}
//else
//{
// Console.WriteLine("不是");
//}
string[] names = { "張三", "李四", "王五", "趙六", "田七" };
////張三|李四|王五|趙六|田七
//string strNew = string.Join("|", "張三","李四","王五","趙六","田七");
string strNew=string.Join("|",names);
Console.WriteLine(strNew);
string Name = "張三";
int Age = 19;
//通過拼接的方式啼县,將字符串和變量拼接成一個長字符串
// string str = "我的名字是" + Name + ",今年" + Age + "歲";
//使用Format()方法材原,可以使代碼簡潔
// string str = string.Format("我的名字是{0},今年{1}歲",Name,Age);
//C#引入了內(nèi)插字符串的語法,使用$"字符串{變量1}字符串{變量2}"
string str = $"我的名字是{Name},今年{Age}歲";
Console.WriteLine(str);
Console.ReadKey();
}
}
StringBuilder類
//String類和StringBuilder類測試
class Program
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
string str = null;
//創(chuàng)建一個計時器季眷,用來記錄程序運行的時間
Stopwatch sw = new Stopwatch();
sw.Start(); //開始計時
for (int i = 0; i < 100000; i++)
{
// str += i;
sb.Append(i);
}
sw.Stop();//結(jié)束計時
Console.WriteLine(sw.Elapsed);//測量運行的總時間
Console.WriteLine(sb.ToString());
Console.ReadKey();
}
}
/*當(dāng)大量進行字符串操作的時候余蟹,比如很多次的字符串的拼接操作
* String對象是不可變的,每次使用System.String類中的方法時子刮,都要在內(nèi)存中創(chuàng)建一個新的字符串對象威酒,這就需要
* 為該新對象分配新的空間窑睁,在需要對字符串執(zhí)行重復(fù)修改的情況下,與創(chuàng)建新的String對象相關(guān)的系統(tǒng)開銷可能會非常大
* 如果要修改字符串不創(chuàng)建新的對象葵孤,則可以使用System.Text.StringBuilder類.
* 例如:當(dāng)在一個循環(huán)中將許多字符串連接在一起時担钮,使用StringBuilder類可以提升性能
* StringBuilder != String (將StringBuilder轉(zhuǎn)換為String用ToString())
*
* StringBuilder sb = new StringBuilder();
* sb.Append() 追加字符串
* sb.ToString() //把StringBuilder轉(zhuǎn)換成字符串
* sb.Insert() //插入
* sb.Replace() 替換
* sb.Remove()刪除
*
*
*
*/
class Program
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
sb.Append("張三");
sb.Append("李四");
sb.Append("王五");
sb.Insert(1, 123);
sb.Replace("李四", "李世民");
sb.Remove(1,3);
Console.WriteLine(sb.ToString());
Console.ReadKey();
}
}
正則表達式
/*
* 正則表達式是用來進行文本處理的技術(shù),跟語言無關(guān)尤仍,在幾乎所有的語言中都有實現(xiàn)
* 它是對文本箫津,對字符串操作的
* 一個正則表達式就是由普通字符以及特殊字符(稱之為元字符)組成的文字模式
*
* 1.正則表達式元字符
* .表示除\n之外的任意的單個字符
* [] 字符組,任意的單個字符宰啦,中括號中的任意一個字符
* a.b
* a[1-9]b
* a[a-zA-Z]b
* a[a-zA-Z0-9]b
* a[axz.]b 當(dāng).出現(xiàn)在中括號中的時候苏遥,本身就表示一個普通的點.,已經(jīng)不再是“元字符”了
*
* | 表示或的意思 "或"的優(yōu)先級非常低,最后才計算
* a(x|y)b
* z|food -> z或者 food
* (z|f)ood -> zood或者food
* () 改變優(yōu)先級和提取組(分組)
*限定符:
*{n} 表示前面的表達式必須出現(xiàn)n次
*{n,}表示前面的表達式至少出現(xiàn)n次绑莺,最多不限
*{n,m}表示至少n次,最多出現(xiàn)m次
*{5,10}
*
* *:表示出現(xiàn)0次或多次惕耕。{0,}
* +:表示出現(xiàn)1次或多次纺裁。{1,}
* ?:表示0次或1次。 {0,1} 還有另一個意思是:終止貪婪模式
* colou?r ->color colour
* (colou)?r -> colour r
*
* ^與$
*
* ^表示字符串的開始
* ^hello
* a[^0-9a-z]b -> 另外一個意思司澎,代表非
* $表示字符串的結(jié)尾
* a$
*
* 2.簡寫表達式
* \d:表示[0-9]
* \D:表示[^0-9]
* a[0-9]b => a\db
*
* \s 表示所有空白符
* \S:表示\s的反面
*
* \w [a-zA-Z0-9_]
* \W 就是\w的反面
* 由于.net默認采用unicode方式來匹配欺缘,所以\w也可以匹配漢字
*
* \b 表示單詞的邊界
*
* Regex.IsMatch 方法用于判斷一個字符串是否匹配正則表達式 (一定不能忘了^和$)
*
*
*
*
*
*/
class Program
{
static void Main(string[] args)
{
#region 案例1
//while (true)
//{
// Console.WriteLine("請輸入一個郵政編碼");
// string postcode = Console.ReadLine();
// //驗證是否是合法的郵政編碼
// //IsMatch()表示只要整個字符串中有任何一部分可以匹配,該正則表達式挤安,則返回true.
// // bool b = Regex.IsMatch(postcode, "[0-9]{6}");
// //要求必須是6個數(shù)字開頭谚殊,并且必須是6個數(shù)字結(jié)尾,所以說就是蛤铜,必須完全匹配
// // bool b = Regex.IsMatch(postcode, "^[0-9]{6}$");
// //bool b = Regex.IsMatch(postcode, @"^\d{6}$");
// //由于.net默認采用unicode匹配方式嫩絮,所以\d也匹配全角的數(shù)字
// //RegexOptions.ECMAScript表示按照ASCII的方式來匹配
// bool b = Regex.IsMatch(postcode, @"^\d{6}$",RegexOptions.ECMAScript);
// Console.WriteLine(b);
#endregion
#region 案例二
//while (true)
//{
// Console.WriteLine("請輸入:");
// string msg = Console.ReadLine();
// bool b = Regex.IsMatch(msg, "z|food");
// //測試時,依次輸入:z(true) food(true) zood(true) chinese food,very good(true) zjskjdsjdj(true)
// Console.WriteLine(b);
//}
#endregion
#region 案例三
//while (true)
//{
// Console.WriteLine("請輸入:");
// string msg = Console.ReadLine();
// //以z開頭或者以food結(jié)尾
// bool b = Regex.IsMatch(msg,"^z|food$");
// Console.WriteLine(b);
//}
#endregion
#region 案例四
while (true)
{
Console.WriteLine("請輸入:");
string msg = Console.ReadLine();
//要么是z 或者是food
bool b = Regex.IsMatch(msg, "^(z|food)$");
Console.WriteLine(b);
}
#endregion
#region 案例五
//要求用戶輸入一個整數(shù)围肥,匹配是否為>=10并且小于等于20de 數(shù)組
while (true)
{
Console.WriteLine("請輸入一個10-20之間的數(shù)剿干,含10與20");
string msg = Console.ReadLine();
//找規(guī)律
bool b = Regex.IsMatch(msg, "^(1[0-9]|20)$");
Console.WriteLine(b);
}
#endregion
Console.ReadKey();
}
}
//正則表達式之Match類和MatchCollection類
class Program
{
static void Main(string[] args)
{
//一個水手出海,想看看他能做些什么
string input = "a sailor went to sea to set ,to see what he could sec ";
//MatchaCollection是Match對象的集合
MatchCollection mc = Regex.Matches(input, @"se\w");
Console.WriteLine($"共找到了{mc.Count}個匹配");
foreach (Match s in mc)
{
Console.WriteLine($"在索引{s.Index}處發(fā)現(xiàn){s.Value}");
}
Console.ReadKey();
}
}