From 92afba7690b57a3b4ad4cddb2fad226c518b490b Mon Sep 17 00:00:00 2001 From: yang-wenrong <956352682@qq.com> Date: Thu, 27 May 2021 17:27:42 +0800 Subject: [PATCH 1/2] 5.27 --- .../Program.cs" | 155 ++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 "\347\254\2544\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/Program.cs" diff --git "a/\347\254\2544\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/Program.cs" "b/\347\254\2544\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/Program.cs" new file mode 100644 index 0000000..e44fd8b --- /dev/null +++ "b/\347\254\2544\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/Program.cs" @@ -0,0 +1,155 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp2 +{ + class Program + { + static void Main(string[] args) + { + //Stopwatch sw = new Stopwatch(); + //sw.Start(); + //string str = ""; + //for (int i = 0; i < 100000; i++) + //{ + // str += i; + //} + //sw.Stop(); + //Console.WriteLine(sw.Elapsed); + + Stopwatch sw = new Stopwatch(); + sw.Start(); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < 1000000; i++) + { + sb.Append(i); + } + sw.Stop(); + Console.WriteLine(sw.Elapsed); + + Console.ReadKey(); + } + + static void Test() + { + //Console.WriteLine("\r\n======================string类======================"); + //string str = "abc"; + //str = str.ToUpper(); + //Console.WriteLine(str); + //str = "JAVA";//Java + //Console.WriteLine(str.ToLower()); + + //string str = "abc"; + string str1 = "abc"; + //string str3 = "c"; + //string str2 = "ab" + str3; + //字符串常量池 + string str4 = "ab" + "c";//string的优化 + + //Console.WriteLine(str.Equals(str1));//true + //Console.WriteLine(str == str1);//true + //Equals 一般比较的是对象内某些字段的值,前提是你重写了它,否在它在父类里,初始的功能比较的其实就是内存地址。 + //== 一般是比较内存地址。除非你重写了它。在string中Equals和==一样 + + //Console.WriteLine(object.ReferenceEquals(str,str1));//true + + //Console.WriteLine(str1 == str2);//True + //Console.WriteLine(object.ReferenceEquals(str1,str2));//false + //Console.WriteLine(object.ReferenceEquals(str4,str1));//true + Console.WriteLine("================================="); + string str = "asdfasfllsdfl.txt"; + //str = str.Replace("as", "阿萨"); + //str = str.Replace("a", "阿"); + //str = str.Replace('a', '阿'); + //Console.WriteLine(str) ; + + //str = str.Substring(2,4); + //Console.WriteLine(str); + + Console.WriteLine(str.IndexOf("df"));//返回第一个位置的索引 + Console.WriteLine(str.LastIndexOf("df"));//返回最后一个位置的索引 + + Console.WriteLine(str.StartsWith("sd")); + Console.WriteLine(str.EndsWith(".txt")); + + str = "闽大|南校区,望云楼|2楼|实训3"; + char[] seprator = { '|', ',' }; + string[] arr = str.Split(seprator); + foreach (var item in arr) + { + Console.Write(item + " "); + } + Console.WriteLine("\r\n" + arr[2]); + + str = ",asdfasf,asdf,sdf,sa,dfas,,df,"; + seprator = new char[] { ',' }; + + arr = str.Split(seprator); + Console.WriteLine(arr.Length);//9 + arr = str.Split(seprator, StringSplitOptions.RemoveEmptyEntries); + Console.WriteLine(arr.Length); + Console.WriteLine("===================="); + Console.WriteLine(str.Contains("aaaas")); + + str = " a b c "; + Console.WriteLine(str.Length); + //str = str.Trim(); + str = str.Replace(" ", ""); + Console.WriteLine(str); + Console.WriteLine(str.Length); + + //IsNullOrEmpty() 判断是否是Null 或者 是否是"" + Console.WriteLine(string.IsNullOrEmpty(""));//true + Console.WriteLine(string.IsNullOrEmpty(null));//true + Console.WriteLine(string.IsNullOrEmpty(" "));//false + + //IsNullOrWhiteSpace() 判断是否是Null 或者 是否是"" 或者是否是空白字符串" " + Console.WriteLine(string.IsNullOrWhiteSpace(null));//true + Console.WriteLine(string.IsNullOrWhiteSpace(""));//true + Console.WriteLine(string.IsNullOrWhiteSpace(" "));//true + + Console.WriteLine(str.Insert(2, "啊")); + + string name = "张三"; + int age = 20; + Console.WriteLine("我的名字叫{0},今年{1}岁", name, age); + + string str5 = "我的名字叫" + name + ",今年" + age + "岁"; + + str5 = string.Format("我的名字叫{0},今年{1}岁", name, age); + + str5 = $"我的名字叫{name},今年{age}岁";//语法糖 + + Console.WriteLine(str5); + + str = "abc"; + //char[] cArray = {'a','b','c'}; + Console.WriteLine(str.Length); + Console.WriteLine(str[2]); + + for (int i = 0; i < str.Length; i++) + { + Console.WriteLine(str[i]); + } + + + StringBuilder sb = new StringBuilder("aaa"); + sb.Append("bbb"); + sb.Append("ccc"); + + Console.WriteLine(sb); + sb.Clear(); + Console.WriteLine(sb); + sb.Append("ddd"); + Console.WriteLine(sb); + sb.Append(1); + sb.Append(true); + + Console.WriteLine(sb); + } + } +} -- Gitee From 258d146d49e0cc0363100ead2196fd235db2d8fc Mon Sep 17 00:00:00 2001 From: yang-wenrong <956352682@qq.com> Date: Thu, 27 May 2021 17:28:41 +0800 Subject: [PATCH 2/2] 5.27 --- .../Program.cs" | 185 ++++++------------ 1 file changed, 56 insertions(+), 129 deletions(-) diff --git "a/\347\254\2544\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/Program.cs" "b/\347\254\2544\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/Program.cs" index e44fd8b..999fa82 100644 --- "a/\347\254\2544\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/Program.cs" +++ "b/\347\254\2544\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/Program.cs" @@ -1,155 +1,82 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ConsoleApp2 +namespace Demo { class Program { static void Main(string[] args) { - //Stopwatch sw = new Stopwatch(); - //sw.Start(); - //string str = ""; - //for (int i = 0; i < 100000; i++) - //{ - // str += i; - //} - //sw.Stop(); - //Console.WriteLine(sw.Elapsed); - - Stopwatch sw = new Stopwatch(); - sw.Start(); - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < 1000000; i++) - { - sb.Append(i); - } - sw.Stop(); - Console.WriteLine(sw.Elapsed); - - Console.ReadKey(); + test3(); } - - static void Test() + public static void test1() { - //Console.WriteLine("\r\n======================string类======================"); - //string str = "abc"; - //str = str.ToUpper(); - //Console.WriteLine(str); - //str = "JAVA";//Java - //Console.WriteLine(str.ToLower()); - - //string str = "abc"; - string str1 = "abc"; - //string str3 = "c"; - //string str2 = "ab" + str3; - //字符串常量池 - string str4 = "ab" + "c";//string的优化 - - //Console.WriteLine(str.Equals(str1));//true - //Console.WriteLine(str == str1);//true - //Equals 一般比较的是对象内某些字段的值,前提是你重写了它,否在它在父类里,初始的功能比较的其实就是内存地址。 - //== 一般是比较内存地址。除非你重写了它。在string中Equals和==一样 + // 一、统计下面一段文字中“类”字和“码”的个数。 - //Console.WriteLine(object.ReferenceEquals(str,str1));//true + //与其他面向对象语言一样,C# 语言也具有面向对象语言的基本特征,即封装、继承、 多态。封装:就是将代码看作一个整体,例如使用类、方法、接口等。在使用定义好的类、 方法、接口等对象时不必考虑其细节,只需要知道其对象名以及所需要的参数即可,也是一种提升代码安全性的方法。继承:是一种体现代码重用性的特性,减少代码的冗余,但在 C# 语言中仅支持单继承。多态:不仅体现了代码的重用性,也体现了代码的灵活性,它主要通过继承和实现接口的方式,让类或接口中的成员表现出不同的作用。 - //Console.WriteLine(str1 == str2);//True - //Console.WriteLine(object.ReferenceEquals(str1,str2));//false - //Console.WriteLine(object.ReferenceEquals(str4,str1));//true - Console.WriteLine("================================="); - string str = "asdfasfllsdfl.txt"; - //str = str.Replace("as", "阿萨"); - //str = str.Replace("a", "阿"); - //str = str.Replace('a', '阿'); - //Console.WriteLine(str) ; - - //str = str.Substring(2,4); - //Console.WriteLine(str); - - Console.WriteLine(str.IndexOf("df"));//返回第一个位置的索引 - Console.WriteLine(str.LastIndexOf("df"));//返回最后一个位置的索引 - - Console.WriteLine(str.StartsWith("sd")); - Console.WriteLine(str.EndsWith(".txt")); - - str = "闽大|南校区,望云楼|2楼|实训3"; - char[] seprator = { '|', ',' }; - string[] arr = str.Split(seprator); - foreach (var item in arr) + //1、使用循环遍历的方法来实现。 + //2、使用Replace()方法来实现。 + //3、使用Split()方法来实现。 + int leicount = 0; + int macount = 0; + string s1 = "与其他面向对象语言一样,C# 语言也具有面向对象语言的基本特征,即封装、继承、 多态。封装:就是将代码看作一个整体,例如使用类、方法、接口等。在使用定义好的类、 方法、接口等对象时不必考虑其细节,只需要知道其对象名以及所需要的参数即可,也是一种提升代码安全性的方法。继承:是一种体现代码重用性的特性,减少代码的冗余,但在 C# 语言中仅支持单继承。多态:不仅体现了代码的重用性,也体现了代码的灵活性,它主要通过继承和实现接口的方式,让类或接口中的成员表现出不同的作用。"; + foreach (char item in s1) { - Console.Write(item + " "); + if (item.Equals('类')) { + leicount++; + } + if (item.Equals('码')) { + macount++; + } } - Console.WriteLine("\r\n" + arr[2]); - - str = ",asdfasf,asdf,sdf,sa,dfas,,df,"; - seprator = new char[] { ',' }; - - arr = str.Split(seprator); - Console.WriteLine(arr.Length);//9 - arr = str.Split(seprator, StringSplitOptions.RemoveEmptyEntries); - Console.WriteLine(arr.Length); - Console.WriteLine("===================="); - Console.WriteLine(str.Contains("aaaas")); - - str = " a b c "; - Console.WriteLine(str.Length); - //str = str.Trim(); - str = str.Replace(" ", ""); - Console.WriteLine(str); - Console.WriteLine(str.Length); + Console.WriteLine("类:{0} 码:{1}",leicount,macount); - //IsNullOrEmpty() 判断是否是Null 或者 是否是"" - Console.WriteLine(string.IsNullOrEmpty(""));//true - Console.WriteLine(string.IsNullOrEmpty(null));//true - Console.WriteLine(string.IsNullOrEmpty(" "));//false + int lei = s1.Length - s1.Replace("类", "").Length; + int ma = s1.Length - s1.Replace("码", "").Length; - //IsNullOrWhiteSpace() 判断是否是Null 或者 是否是"" 或者是否是空白字符串" " - Console.WriteLine(string.IsNullOrWhiteSpace(null));//true - Console.WriteLine(string.IsNullOrWhiteSpace(""));//true - Console.WriteLine(string.IsNullOrWhiteSpace(" "));//true + Console.WriteLine("类:{0} 码:{1}",lei,ma); - Console.WriteLine(str.Insert(2, "啊")); - string name = "张三"; - int age = 20; - Console.WriteLine("我的名字叫{0},今年{1}岁", name, age); + string[] a = { "类" }; + string[] result = s1.Split(a, StringSplitOptions.None); + Console.WriteLine("字符串中含有类的个数为:" + (result.Length - 1)); + string[] b = { "码" }; + string[] result2 = s1.Split(b, StringSplitOptions.None); + Console.WriteLine("字符串中含有码的个数为:" + (result2.Length - 1)); - string str5 = "我的名字叫" + name + ",今年" + age + "岁"; - - str5 = string.Format("我的名字叫{0},今年{1}岁", name, age); - - str5 = $"我的名字叫{name},今年{age}岁";//语法糖 - - Console.WriteLine(str5); - - str = "abc"; - //char[] cArray = {'a','b','c'}; - Console.WriteLine(str.Length); - Console.WriteLine(str[2]); - - for (int i = 0; i < str.Length; i++) - { - Console.WriteLine(str[i]); - } - - - StringBuilder sb = new StringBuilder("aaa"); - sb.Append("bbb"); - sb.Append("ccc"); - - Console.WriteLine(sb); - sb.Clear(); - Console.WriteLine(sb); - sb.Append("ddd"); - Console.WriteLine(sb); - sb.Append(1); - sb.Append(true); + } + public static void test2() + { + // 二、 + //C# (英 文名为 CSharp) 是 微 软开发的一种 面向对 象的 编程 语言。C# 语言具备了面向对象 语言 的特 征, 即封装、继承、多态,并且添加了 事件和委托,增强了 编程的灵 活性。C# 语 言是 一种安全的、稳定的、简 单 的、面向对象的编程 语言 ,其语 法与 C++ 类似,但在编程过 程中要比 C++ 简单;它不仅去掉了 C++ 和 Java 语 言中的一些复杂特性,还提 供了可视化 工具,能够高效地 编写程序。C# 是运行 在.NE T平台之上的 编程 语言。 - Console.WriteLine(sb); + //去掉上面一段文字的所有空格,并统计空格数。 + String s1 = "C# (英 文名为 CSharp) 是 微 软开发的一种 面向对 象的 编程 语言。C# 语言具备了面向对象 语言 的特 征, 即封装、继承、多态,并且添加了 事件和委托,增强了 编程的灵 活性。C# 语 言是 一种安全的、稳定的、简 单 的、面向对象的编程 语言 ,其语 法与 C++ 类似,但在编程过 程中要比 C++ 简单;它不仅去掉了 C++ 和 Java 语 言中的一些复杂特性,还提 供了可视化 工具,能够高效地 编写程序。C# 是运行 在.NE T平台之上的 编程 语言。"; + int count = s1.Length - s1.Replace(" ", "").Length; + Console.WriteLine("空格个数为:{0}",count); + } + public static void test3() { + + //三、在控制台下输入你的姓名、年龄、家庭住址和兴趣爱好,使用StringBuilder类把这些信息连接起来并输出。 + Console.WriteLine("请输入你的姓名"); + string name = Console.ReadLine(); + Console.WriteLine("请输入你的年龄"); + string age = Console.ReadLine(); + Console.WriteLine("请输入你的家庭住址"); + string home = Console.ReadLine(); + Console.WriteLine("请输入你的兴趣爱好"); + string hobby = Console.ReadLine(); + + StringBuilder s1 = new StringBuilder(); + s1.Append(name+" "); + s1.Append(age + " "); + s1.Append(home + " "); + s1.Append(hobby+" "); + Console.WriteLine(s1); } } } -- Gitee