Lession10 常用类(正则表达式、Date Time结构、string类、Math类)

Lession10 常用类(正则表达式、Date Time结构、string类、Math类)注意引用命名空间 usingSystem Text 15 LastIndexOf 判断某个字符串在字符串中最后一次出现的位置 如果没有返回 1

大家好,欢迎来到IT知识分享网。

Date Time结构:

using System; namespace ConsoleApp1 //Date_Time_结构 { class Program { static void Main(string[] args) { //例子;使用DateTime获取当前时间,分别输出该日是当月的第几天,星期几,及一年中的第几天 //并计算30天后的日期 DateTime dt = DateTime.Now; Console.WriteLine("当前日期为:{0}", dt.ToLongTimeString()); Console.WriteLine("当前是本月的第{0}天", dt.Day); Console.WriteLine("当前是星期{0}", dt.DayOfWeek); Console.WriteLine("当前是本年度的第{0}天", dt.DayOfYear); Console.WriteLine("30天后的日期是{0}", dt.AddDays(30)); Console.WriteLine("---------------------------------------"); //两个日期的差可以由TimeSpan的对象来存放 假设计算现在距离2021年5月1日的天数 DateTime dt2 = new DateTime(2021, 5, 1); TimeSpan ts = dt2 - dt; Console.WriteLine("间隔的天数为:{0}天", ts.Days); Console.ReadKey(); } } } 

Lession10 常用类(正则表达式、Date Time结构、string类、Math类)

Math类: 

using System; namespace ConsoleApp1 //_Math类 { /* * 圆周率: * Abs():取绝对值 * Ceiling(): 向上取整数(就是说小数点后面只要有非0的值,就直接+1取整) * Floor():向下取整数(就是说,小数点后面不管是什么值,都返回整数这个值) * Round():四舍五入 * Max():返回两个数中的最大值 * Min():返回两个数中的最小值 * Equals():返回指定对象实例是否相等 */ class Program { static void Main(string[] args) { //圆周率 Console.WriteLine(Math.PI); //求绝对值 Console.WriteLine(Math.Abs(-2)); Console.WriteLine(Math.Abs(-2.1F)); Console.WriteLine(Math.Abs(10)); //ceiling() 向上取整数(就是说小数点后面只要有非0的值,就直接+1取整) Console.WriteLine(Math.Ceiling(33.0)); Console.WriteLine(Math.Ceiling(33.1)); Console.WriteLine(Math.Ceiling(33.8)); Console.WriteLine("----------------------------"); //Floor()向下取整数(就是说,小数点后面不管是什么值,都返回整数这个值) Console.WriteLine(Math.Floor(33.0)); Console.WriteLine(Math.Floor(33.1)); Console.WriteLine(Math.Floor(33.8)); Console.WriteLine("----------------------------"); //Round() 四舍五入 Console.WriteLine(Math.Round(33.0)); Console.WriteLine(Math.Round(33.2)); Console.WriteLine(Math.Round(33.6)); Console.WriteLine("----------------------------------"); Console.WriteLine(Math.Equals(11, 12)); Console.WriteLine(Math.Equals(11, 11)); Console.WriteLine("----------------------------------"); Console.WriteLine("请输入第一个整数:"); int num1 = int.Parse(Console.ReadLine()); Console.WriteLine("请输入第二个整数:"); int num2 = int.Parse(Console.ReadLine()); Console.WriteLine("两个数的最大值是{0}", Math.Max(num1, num2)); Console.WriteLine("两个数的最小值是{0}", Math.Min(num1, num2)); Console.ReadKey(); } } } 

Lession10 常用类(正则表达式、Date Time结构、string类、Math类)

 

Random类:

 

using System; namespace ConsoleApp1 //Random类 { /* * 要使用Random类的话,先实例化 * Next():每次产生一个不同的随机整数 * Next(int maxValue):产生一个比max value小的正整数 * Next(int minValue,int maxValue):产生一个minValue~maxValue的正整数,但不包括maxVlue * NextDouble():产生0.0~1.0的浮点数 * NextBytes(byte[] buffer)用随机数填充字节数的数组 * * */ class Program { static void Main(string[] args) { //Random随机数类 Random rd = new Random(); Console.WriteLine("产生一个随机的整数:{0}", rd.Next()); Console.WriteLine("产生一个比30小的正整数:{0}", rd.Next(30)); Console.WriteLine("产生一个10以内的随机数:{0}", rd.Next(0, 11)); Console.WriteLine("产生一个0到1之间的浮点数:{0}", rd.NextDouble()); byte[] b = new byte[5];//byte:0-255 sbyte:-128~127 rd.NextBytes(b); foreach (byte i in b) { Console.Write(i + " "); } Console.ReadKey(); } } } 

Lession10 常用类(正则表达式、Date Time结构、string类、Math类)

 String Builder类:

using System; using System.Text; namespace ConsoleApp //stringBuilder类 { /*StringBuilder sb = new StringBuilder(); (注意引用命名空间 using System.Text;) * sb.Append():追加字符串 * sb.ToString():把StringBuilder转换成String类型 * 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(3, "王拓"); sb.Replace("王拓", "李琴琴"); sb.Remove(3, 3); Console.WriteLine(sb.ToString()); Console.ReadKey(); } } } 

Lession10 常用类(正则表达式、Date Time结构、string类、Math类) 

String类: 

using System; using System.Text; namespace ConsoleApp1 //String类 { class Program { static void Main(string[] args) { //@符号:取消它在字符中的转义作用 // string rou = @"E:\net\demo"; // Console.WriteLine(rou); // //将字符串按照原格式输出 // string words = @"今天天气不好, // 但是大家的学习热情很高"; // Console.WriteLine(words); // Console.WriteLine("---------------------------"); // string s = "abcdefg"; 我们可以将字符串看做是一个char类型的一个只读数组 // Console.WriteLine(s[0]); // //首先吧字符串转换成char类型的数组 // char[] chs = s.ToCharArray(); // chs[0] = 'b'; //将char数组再转换成字符串 //s = new string(chs); //Console.WriteLine(s); //Console.WriteLine("s的字符串长度是{0}",s.Length); //练习:输入一个你心中想的那个人的名字,然后输出一个它的字符长度 //Console.WriteLine("请输入你心中想的那个人的名字:"); //string name = Console.ReadLine(); //Console.WriteLine("你心中想的那个人的名字的长度为{0}", name.Length); //Console.WriteLine("请输入你喜欢的课程:"); // string lessionOne = Console.ReadLine(); // //将字符串转换成大写 // // lessionOne = lessionOne.ToUpper(); // //将字符串转换成小写 lessionOne = lessionOne.ToLower(); // Console.WriteLine("请再输入你喜欢的课程:"); // string lessionTwo = Console.ReadLine(); // //lessionTwo = lessionTwo.ToLower(); // //if (lessionOne == lessionTwo) // //{ // // Console.WriteLine("真巧,咱俩都喜欢这个课程"); // //} // //else // //{ // // Console.WriteLine("哎,咱俩喜欢的不一样"); // //} // // Console.WriteLine(lessionOne); // if (lessionOne.Equals(lessionTwo,StringComparison.OrdinalIgnoreCase)) // { // Console.WriteLine("真巧,咱俩都喜欢这个课程"); // } // else // { // Console.WriteLine("哎,咱俩喜欢的不一样"); // } //string str = " 哈哈小笨蛋 "; //Console.WriteLine(str.Length); //Console.WriteLine(str.Trim().Length); //Console.WriteLine(str.TrimStart().Length); //Console.WriteLine(str.TrimEnd().Length); Console.WriteLine("------------------------------------"); // string s = "a b dfdf _ + = ,,, fada"; //分割字符串Split() //char[] chs = {' ','_','+','=',',' }; //string[] str = s.Split(chs,StringSplitOptions.RemoveEmptyEntries); //练习:从日期字符串("2021-04-07")中分割出年月日,输出:2021年04月07日 //string s = "2021-04-07"; //char[] chs = { '-' }; //string[] date = s.Split(chs, StringSplitOptions.RemoveEmptyEntries); //Console.WriteLine("{0}年{1}月{2}日",date[0],date[1],date[2]); //练习Substring():截取 字符串 // string str = "今天天气好晴朗,处处好风光"; // str = str.Substring(1); //str = str.Substring(2,2); //Console.WriteLine(str); //练习Contains(),Replace() //string str = "物联网一班牛逼"; //if (str.Contains("牛逼")) //{ // str = str.Replace("牛逼",""); //} //Console.WriteLine(str); //练习StartsWith(),EndsWith() //string str = "今天天气好晴朗,处处好风光"; //if (str.EndsWith("光")) //{ // Console.WriteLine("是的"); //} //else //{ // Console.WriteLine("不是"); //} //练习IndexOf(),LastIndexOf() //string str = "今天天气好晴朗,处处好风光"; //int index= str.IndexOf('处'); //int lastIndex = str.LastIndexOf("处"); //int index2 = str.IndexOf('天',2); //int index3 = str.IndexOf('一'); //Console.WriteLine(index); //Console.WriteLine(lastIndex); //Console.WriteLine(index2); //Console.WriteLine(index3); //练习string.Join() //string[] names = { "屈增辉", "李佳敏", "何帅", "李琴琴", "李心怡", "赵文凤", "张艺明" }; 屈增辉|李佳敏|何帅|李琴琴|李心怡|赵文凤|张艺明 string strNew = string.Join("|", "屈增辉", "李佳敏", "何帅", "李琴琴", "李心怡", "赵文凤", "张艺明"); //string strNew = string.Join("-",names); //Console.WriteLine(strNew); //练习 string.IsNullOrEmpty() //string str = ""; //if (string.IsNullOrEmpty(str)) //{ // Console.WriteLine("是的"); //} //else //{ // Console.WriteLine("不是"); //} //练习 string.Format() string name = "张艺明"; int age = 19; //string str = "我的名字是" + name + ",今年" + age + "岁"; //使用Format() // string str = string.Format("我的名字是{0},今年{1}岁", name, age); //使用$ string str = $"我的名字是{name},今年{age}岁"; string re = name.Remove(3, 5); Console.WriteLine("re的值是" + re); Console.WriteLine(str); Console.WriteLine("---------------------------------"); string path = @"c:\a\b\c\fsd\dsad\ewrewr\dfgfdg\好看的视频.mp4"; //1.找到最后一个\的位置 int index = path.LastIndexOf("\\");//双斜线的第一个斜线是转义符,转义成“\”单斜线的意思 //2.截取 path = path.Substring(index + 1); Console.WriteLine("path的值是:" + path); Console.ReadKey(); } } } 

 String类和StringBuilder类测试:

using System; using System.Diagnostics; using System.Text; namespace ConsoleApp1 //String类和StringBuilder类测试 { class Program { static void Main(string[] args) { StringBuilder sb = new StringBuilder(); // string str = null; //创建一个计时器,用来记录程序运行的时间 Stopwatch sw = new Stopwatch(); sw.Start(); //开始计时 for(int i = 0;i < ; i++) { // str += i; sb.Append(i);//添加 } sw.Stop();//结束计时、 Console.WriteLine(sw.Elapsed);//测量运行的总时间 Console.WriteLine(sb.ToString()); Console.ReadKey(); } } } 

Lession10 常用类(正则表达式、Date Time结构、string类、Math类)

正则表达式: 

using System; using System.Text.RegularExpressions; namespace ConsoleApp1 //正则表达式 { /* * 正则表达式是用来进行文本处理的技术,跟语言无关,在几乎所有的语言中都有实现 * 它是对文本,对字符串操作的 * 一个正则表达式就是由普通字符以及特殊字符(元字符)组成的文字模式 * * 1.正则表达式的元字符: * A:.表示除\n之外的任意的单个字符 * B: []字符组,任意的单个字符,中括号中的任意一个字符 * * a.b ->avb * a[1-9]b ->a5b * a[xyz]b ->axb ayb azb * a[a-zA-Z0-9]b * a[axz.]b a.b * * C:| 表示或的意思,但是“或”优先级非常低,最后才算 * * z|food-> z或者food * (z|f)ood -> zood 或者food * * D:()改变优先级的 * * 限定符: * E:{n}:表示前面的表达式必须出现n次 * F:{n,}:表示前面的表达式至少出现n次,最多不限 * G:{n,m}:表示前面的表达式至少出现n次,最多出现m次 * {5,10}{5}{5,} * * H.*:表示出现0次或者多次 {0,} * I.+:表示出现1次或者多次 {1,} * J.?:表示0次或者1次 {0,1} * * colou?r ->color colour * (colou)?r ->r colour * * ^与$ * * k. ^表示字符串的开始 * L. $表示字符串的结尾 * ^hello * a[^0-9a-z] ->这是另一个意思,代表非 * a$ * * 简写表达式 * \d:表示[0-9] * \D:表示[^0-9] * a[0-9]b => a\db * * \s 表示所有空白符 * \S 表示\s的反面 * * \w [a-zA-Z0-9] * \W 就是\w的反面 * * * Regex.IsMatch方法用于判断一个字符串是否匹配正则表达式 (一定不能忘了^和$) * * * */ class Program { static void Main(string[] args) { #region 案例一 //while (true) //{ // Console.WriteLine("请输入一个邮政编码:"); // string postCode = Console.ReadLine(); // //验证是否是合法的邮政编码 // //IsMatch()表示重要整个字符串中有任何一部分可以匹配该正则表达式,则返回true // //[0-9]{6} :6个0到9的数字 // //bool b = Regex.IsMatch(postCode,"^[0-9]{6}$"); // bool b = Regex.IsMatch(postCode, "^\\d{6}$"); // //要求必须是6个数字开头,并且必须是6个数字结尾,所以说就是,必须完全匹配 // Console.WriteLine(b); // //} #endregion #region 案例二 //while (true) //{ // Console.WriteLine("请输入:"); // string msg = Console.ReadLine(); // bool b = Regex.IsMatch(msg, "^((z|food)$"); // Console.WriteLine(b); //} #endregion #region 案例三 //要求用户输入一个整数,匹配是否为 >=10并且<=20的数 while (true) { Console.WriteLine("请输入一个10—20之间的整数,含10和20:"); string msg = Console.ReadLine(); // 1[0-9]|20 bool b = Regex.IsMatch(msg,"^1[0-9]|20$"); Console.WriteLine(b); } #endregion Console.ReadKey(); } } } 

Lession10 常用类(正则表达式、Date Time结构、string类、Math类) 

正则表达式之Match类和MatchCollection类:

using System; using System.Text.RegularExpressions; namespace ConsoleApp1 //正则表达式之Match类和MatchCollection类 { class Program { static void Main(string[] args) { string input = "a sailor went to sea to set,to see what he could sec"; //MatchCollection是Macth对象的集合 MatchCollection mc = Regex.Matches(input, @"se\w"); Console.WriteLine($"共找到了{mc.Count}个匹配"); foreach (Match s in mc) { Console.WriteLine($"在索引{s.Index}处发现{s.Value}"); } Console.ReadKey(); } } } 

Lession10 常用类(正则表达式、Date Time结构、string类、Math类)

 正则练习:

 

using System; using System.Text.RegularExpressions; namespace ConsoleApp1 //正则练习 { class Program { static void Main(string[] args) { while (true) { Console.WriteLine("请输入一个合法的邮箱地址:"); string email = Console.ReadLine(); bool b = Regex.IsMatch(email, "^[0-9a-zA-Z_-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z]+){1,2}$"); Console.WriteLine(b); } } } } 

Lession10 常用类(正则表达式、Date Time结构、string类、Math类) 

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/132887.html

(0)
上一篇 2025-07-27 21:45
下一篇 2025-03-08 11:10

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信