添加Lesson24
This commit is contained in:
@@ -57,6 +57,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson23_面向对象相关
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson23_面向对象相关-String练习", "Lesson23_面向对象相关-String练习\Lesson23_面向对象相关-String练习.csproj", "{E35D8933-50D7-496A-8E4C-06DDD1CC0878}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson24_面向对象相关-StringBuilder", "Lesson24_面向对象相关-StringBuilder\Lesson24_面向对象相关-StringBuilder.csproj", "{25CCA649-1258-41DA-A5C1-B0C3DE89E2B6}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson24_面向对象相关-StringBuilder练习", "Lesson24_面向对象相关-StringBuilder练习\Lesson24_面向对象相关-StringBuilder练习.csproj", "{4FFD2030-9A14-4F59-A7B8-D218C551E489}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -171,6 +175,14 @@ Global
|
||||
{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
|
||||
{25CCA649-1258-41DA-A5C1-B0C3DE89E2B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{25CCA649-1258-41DA-A5C1-B0C3DE89E2B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{25CCA649-1258-41DA-A5C1-B0C3DE89E2B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{25CCA649-1258-41DA-A5C1-B0C3DE89E2B6}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{4FFD2030-9A14-4F59-A7B8-D218C551E489}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{4FFD2030-9A14-4F59-A7B8-D218C551E489}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{4FFD2030-9A14-4F59-A7B8-D218C551E489}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{4FFD2030-9A14-4F59-A7B8-D218C551E489}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>Lesson24_面向对象相关_StringBuilder</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,71 @@
|
||||
using System.Text;
|
||||
|
||||
namespace Lesson24_面向对象相关_StringBuilder
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
#region 知识回归
|
||||
//string是一个比较特殊的引用类型
|
||||
//每次赋值或修改,都会创建一个新的堆内存
|
||||
//如果频繁修改字符串,会产生大量垃圾对象,影响性能
|
||||
#endregion
|
||||
|
||||
#region 知识点 StringBuilder
|
||||
//C#提供的一个用于处理字符串的公共类
|
||||
//主要解决的问题是:
|
||||
//修改字符串而不创建新的对象,需要频繁修改和拼接字符串可以使用他,可以提升性能
|
||||
//使用前 需要引用命名空间
|
||||
#endregion
|
||||
|
||||
#region 初始化 直接指明内容
|
||||
StringBuilder str = new StringBuilder("Hello");//初始化时可以指定容量 eg:设置100的容量StringBuilder str = new StringBuilder("Hello",100);
|
||||
Console.WriteLine(str);
|
||||
#endregion
|
||||
|
||||
#region 容量
|
||||
//StringBuilder存在一个容量的问题,每次往里添加内容时,如果超过了容量,就会自动扩容
|
||||
//获得容量
|
||||
Console.WriteLine(str.Capacity);
|
||||
//获得字符长度
|
||||
#endregion
|
||||
|
||||
#region 增删查改
|
||||
//增 Append
|
||||
str.Append(",World");
|
||||
Console.WriteLine(str);
|
||||
//增 AppendFormat
|
||||
str.AppendFormat("! I am {0}, age {1}", "Tom", 20);
|
||||
Console.WriteLine(str);
|
||||
//删 Remove
|
||||
str.Remove(5, 24);//从索引5开始删除6个字符
|
||||
Console.WriteLine(str);
|
||||
//查 ToString
|
||||
Console.WriteLine(str.ToString());
|
||||
//改 Replace
|
||||
str[0] = 'h';//通过索引修改单个字符
|
||||
Console.WriteLine(str);
|
||||
str.Replace("hello", "HELLO");//通过Replace修改字符串
|
||||
Console.WriteLine(str);
|
||||
//插 Insert
|
||||
str.Insert(5, ", C#");//在索引5处插入字符串
|
||||
Console.WriteLine(str);
|
||||
//清空 Clear
|
||||
str.Clear();
|
||||
Console.WriteLine(str);
|
||||
|
||||
//stringbuilder的其他方法
|
||||
//Equals
|
||||
if(str.Equals("hello"))
|
||||
{
|
||||
Console.WriteLine("相等");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("不相等");
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>Lesson24_面向对象相关_StringBuilder练习</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,26 @@
|
||||
namespace Lesson24_面向对象相关_StringBuilder练习
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
#region 练习
|
||||
//1.请描述String和StringBuilder的区别
|
||||
//string比stringbuilder更容易产生垃圾 频繁修改会产生大量垃圾对象,影响性能
|
||||
//string比stringbuilder更加灵活 方法更多
|
||||
//如何选择string和stringbuilder
|
||||
//需要频繁修改时,使用stringbuilder
|
||||
//不需要频繁修改时或需要一些方法来处理麻烦的逻辑时,使用string
|
||||
|
||||
|
||||
//2.如何优化内存
|
||||
//两个方面:
|
||||
//如何节约
|
||||
//如何减少GC(垃圾回收)
|
||||
//少new对象就少产生垃圾
|
||||
//合理使用static
|
||||
//合理使用string和stringbuilder
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user