diff --git a/C#核心/C#核心.sln b/C#核心/C#核心.sln index c591175..6810841 100644 --- a/C#核心/C#核心.sln +++ b/C#核心/C#核心.sln @@ -53,6 +53,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson22_面向对象相关 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson22_面向对象相关-万物之父中的方法练习", "Lesson22_面向对象相关-万物之父中的方法练习\Lesson22_面向对象相关-万物之父中的方法练习.csproj", "{0AF0A54F-67BE-46A2-B79D-813ACB6A9EF3}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson23_面向对象相关-String", "Lesson23_面向对象相关-String\Lesson23_面向对象相关-String.csproj", "{24664BCD-DCDC-4DFC-B597-E22D98C3A206}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson23_面向对象相关-String练习", "Lesson23_面向对象相关-String练习\Lesson23_面向对象相关-String练习.csproj", "{E35D8933-50D7-496A-8E4C-06DDD1CC0878}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -159,6 +163,14 @@ Global {0AF0A54F-67BE-46A2-B79D-813ACB6A9EF3}.Debug|Any CPU.Build.0 = Debug|Any CPU {0AF0A54F-67BE-46A2-B79D-813ACB6A9EF3}.Release|Any CPU.ActiveCfg = Release|Any CPU {0AF0A54F-67BE-46A2-B79D-813ACB6A9EF3}.Release|Any CPU.Build.0 = Release|Any CPU + {24664BCD-DCDC-4DFC-B597-E22D98C3A206}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {24664BCD-DCDC-4DFC-B597-E22D98C3A206}.Debug|Any CPU.Build.0 = Debug|Any CPU + {24664BCD-DCDC-4DFC-B597-E22D98C3A206}.Release|Any CPU.ActiveCfg = Release|Any CPU + {24664BCD-DCDC-4DFC-B597-E22D98C3A206}.Release|Any CPU.Build.0 = Release|Any CPU + {E35D8933-50D7-496A-8E4C-06DDD1CC0878}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E35D8933-50D7-496A-8E4C-06DDD1CC0878}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E35D8933-50D7-496A-8E4C-06DDD1CC0878}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E35D8933-50D7-496A-8E4C-06DDD1CC0878}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/C#核心/Lesson23_面向对象相关-String/Lesson23_面向对象相关-String.csproj b/C#核心/Lesson23_面向对象相关-String/Lesson23_面向对象相关-String.csproj new file mode 100644 index 0000000..df5c0a6 --- /dev/null +++ b/C#核心/Lesson23_面向对象相关-String/Lesson23_面向对象相关-String.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + Lesson23_面向对象相关_String + enable + enable + + + diff --git a/C#核心/Lesson23_面向对象相关-String/Program.cs b/C#核心/Lesson23_面向对象相关-String/Program.cs new file mode 100644 index 0000000..cb372e3 --- /dev/null +++ b/C#核心/Lesson23_面向对象相关-String/Program.cs @@ -0,0 +1,96 @@ +namespace Lesson23_面向对象相关_String +{ + internal class Program + { + static void Main(string[] args) + { + #region 知识点一 字符串指定位置获取 + //string是一个密封类,不能被继承 + //字符串的本质是一个char数组 + string str = "123"; + Console.WriteLine(str[0]);//一个类能通过中括号访问,那么这个类就实现了索引器 + + //转换为char数组 + char[] chars = str.ToCharArray(); + Console.WriteLine(chars[1]); + for (int i = 0; i < chars.Length; i++) + { + Console.Write(chars[i]); + } + Console.WriteLine(); + #endregion + + #region 知识点二 字符串拼接 + str = string.Format("{0},{1}", 1, 2); + Console.WriteLine(str); + str = "123" + "456"; + Console.WriteLine(str); + Console.WriteLine(); + #endregion + + #region 知识点三 正向查找字符位置 + str = "abcdefg"; + int index = str.IndexOf("b"); + Console.WriteLine(index); + index = str.IndexOf("noindex"); + Console.WriteLine(index);//找不到返回-1 + + #endregion + + #region 知识点四 反向查找指定字符串位置 + str = "123321"; + index = str.IndexOf("2");//正向查找第一个2 + Console.WriteLine(index); + index = str.LastIndexOf("2"); + Console.WriteLine(index);//反向查找第一个2 + Console.WriteLine(); + #endregion + + #region 知识点五 移除指定位置后的字符 + str = "1234567890"; + Console.WriteLine(str); + str = str.Remove(5);//移除指定位置后的字符 + Console.WriteLine(str); + + str = str.Remove(1, 3);//从指定位置开始移除指定个数的字符 + //从第1个位置往后移除3个字符 + Console.WriteLine(str); + Console.WriteLine(); + #endregion + + #region 知识点六 替换指定字符串 + str = "abcdefg"; + str = str.Replace("cd", "CD"); + Console.WriteLine(str); + Console.WriteLine(); + #endregion + + #region 知识点七 大小写转换 + str = "abcDEF"; + str = str.ToUpper(); + Console.WriteLine(str); + str = str.ToLower(); + Console.WriteLine(str); + Console.WriteLine(); + #endregion + + #region 知识点八 字符串截取 + str = "1234567890"; + str = str.Substring(3);//从指定位置开始截取到字符串末尾 + Console.WriteLine(str); + str = "1234567890"; + str = str.Substring(3,5);//从指定位置开始截取指定个数的字符 + Console.WriteLine(str); + #endregion + + #region 知识点九 字符串分割 + str = "1,2,3,4,5,6"; + string[] strArray = str.Split(',');//根据指定字符分割字符串,返回字符串数组 + foreach (var item in strArray) + { + Console.WriteLine(item); + } + #endregion + } + } +} diff --git a/C#核心/Lesson23_面向对象相关-String练习/Lesson23_面向对象相关-String练习.csproj b/C#核心/Lesson23_面向对象相关-String练习/Lesson23_面向对象相关-String练习.csproj new file mode 100644 index 0000000..0053008 --- /dev/null +++ b/C#核心/Lesson23_面向对象相关-String练习/Lesson23_面向对象相关-String练习.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + Lesson23_面向对象相关_String练习 + enable + enable + + + diff --git a/C#核心/Lesson23_面向对象相关-String练习/Program.cs b/C#核心/Lesson23_面向对象相关-String练习/Program.cs new file mode 100644 index 0000000..a55fe51 --- /dev/null +++ b/C#核心/Lesson23_面向对象相关-String练习/Program.cs @@ -0,0 +1,72 @@ + namespace Lesson23_面向对象相关_String练习 +{ + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("string练习题"); + #region 练习题一 + //请写出string中提供的截取和替换对应的函数名 + string str = "123"; + str = str.Substring(1); + str = str.Substring(1, 1); + + str = "123123"; + str = str.Replace("123", "哈哈哈"); + #endregion + + #region 练习题二 + string str1 = "1|2|3|4|5|6|7"; + string[] strs = str1.Split('|'); + str1 = ""; + for (int i = 0; i < strs.Length; i++) + { + str1 += int.Parse(strs[i]) + 1; + if(i < strs.Length - 1) + { + str1 += "|"; + } + } + Console.WriteLine(str1); + #endregion + + #region 练习题四 + string strr = null;//开了栈内存1,没开堆内存0 + strr = "123";//堆内存+1 + string str2 = strr;//栈内存+1 + str2 = "321";//堆内存+1 + str2 += "123";//堆内存+1 + // 请问,上面这段代码,分配了多少个新的堆空间 + //3个 + #endregion + + #region 练习题五 + //编写一个函数,将输入的字符串反转。不要使用中间商,你必须原地修改输入数组。交换过程中不使用额外空间 + //比如:输入{'h','e','l','l','o'} + //输出 {'o','l','l','e','h'} + + Console.WriteLine("请输入内容"); + string str3 = Console.ReadLine(); + char[] chars = str3.ToCharArray(); + for (int i = 0; i < chars.Length / 2; i++) + { + //交换 + //char temp = chars[i]; + //chars[i] = chars[chars.Length - 1 - i]; + //chars[chars.Length - 1 - i] = temp; + chars[i] = (char)(chars[i] + chars[chars.Length - 1 - i]); + chars[chars.Length - 1 - i] = (char)(chars[i] - chars[chars.Length - 1 - i]); + chars[i] = (char)(chars[i] - chars[chars.Length - 1 - i]); + } + + for (int i = 0; i < chars.Length; i++) + { + Console.Write(chars[i]); + } + + str3 = new string(chars); + Console.WriteLine(str3); + #endregion + } + } +}