添加Lesson23

This commit is contained in:
2025-09-17 11:21:19 +08:00
parent 839ff77b59
commit 1c4099eb8e
5 changed files with 202 additions and 0 deletions
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Lesson23_面向对象相关_String</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
@@ -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
}
}
}