首页 > 编程知识 正文

【Unity 04】C# StringBuilder类及其使用方法

时间:2023-05-06 01:07:46 阅读:286257 作者:3819

PS:本系列笔记将会记录我此次在北京学习Unity开发的总体过程,方便后期写总结,笔记为日更。
笔记内容均为 自己理解,会有遗漏处,不保证每个都对,仅供参考

C#笔记未按照难度排列

String类:

String不能被修改,每次使用String类时都要在内存中重新申请一个新的内存空间,若程序中需要用到大量的字符串修改操作,则会导致内存空间的大量消耗,所以引入StringBuilder类

StringBuilder类:
获得StringBuilder类的长度:

string str = "Hello"; System.Text.StringBuilder sTB = new System.Text.StringBuilder(str); /******************************************************************/ //Length //获得StringBuilder的长度 Console.WriteLine("This is the Length " + sTB.Length); //输出结果为This is the Length 5 /******************************************************************/

StringBuilder的主要方法:

方法名作用Append()方法将新的字符串对象添加到已拥有的StringBuilder对象的末尾AppendFormat(string format, object)方法将文本添加到StringBuilder对象的末尾并且实现IFormattable接口Insert(int index, string value)方法在StringBuilder对象的 指定位置 index 插入 字符文本Remove(int startIndex, int length)方法表示从下标为startIndex处开始移除length个字符Replace(string oldValue, string newValue)方法将字符串中 所有的 等于 oldValue 的地方全部替换成为 newValueClear()方法清空StringBuilder内的所有内容

具体例子:
Append:

/*****************************************************************/ string str = "Hello"; System.Text.StringBuilder sTB = new System.Text.StringBuilder(str); //Append()方法 //将新的字符串对象添加到已拥有的StringBuilder对象的末尾 //将会申请新的空间 string tmpStr = " World!"; sTB.Append(tmpStr); Console.WriteLine("This is the Append " + sTB.ToString()); //输出结果为 This is the Append Hello World! /*****************************************************************/

AppendFormat(string format, object)方法:

/*****************************************************************/ //AppendFormat(string format, object)方法 //将文本添加到StringBuilder对象的末尾并且实现IFormattable接口 //可以自定义变量格式,将其添加到StringBuilder对象的末尾 float myFloat = 3.3112240808f; sTB = new System.Text.StringBuilder("BirthDay is "); sTB.AppendFormat("{0:F6}", myFloat); //将想要添加的文本格式定义为保留小数点后6位 Console.WriteLine("This is the AppendFormat " + sTB.ToString()); //输出结果为 This is the AppendFormat BirthDay is 3.31124 /*****************************************************************/

Insert(int index, string value)方法:

/*****************************************************************/ //insert(int index, string value)方法 //在StringBuilder对象的 指定位置 index 插入 字符文本 sTB = new System.Text.StringBuilder("Hello World!"); sTB.Insert(5, " new "); //在长度下标为5的地方插入“ new ”字符串,即在 Hello 后面插入 Console.WriteLine("This is the insert " + sTB.ToString()); //输出结果为 This is the insert Hello new World /*****************************************************************/

Remove(int startIndex, int length)方法:

/*****************************************************************/ //remove(int startIndex, int length)方法 //表示从下标为startIndex处开始移除length个字符 //沿用上个函数的StringBuilder 即 Hello new World sTB.Remove(5, 4); //从Hello后开始移除 移除4个字符为“ new” Console.WriteLine("This is the Remove " + sTB.ToString()); //输出结果为 This is the Remove Hello World! /*****************************************************************/

Replace(string oldValue, string newValue)方法:

/*****************************************************************/ //Replace(string oldValue, string newValue)方法 //将字符串中 所有的 等于 oldValue 的地方全部替换成为 newValue //可以替换单词 也可以替换字符 sTB = new System.Text.StringBuilder("He??o Wor?d!"); sTB.Replace("?", "l"); //将 "?"替换为 "l" Console.WriteLine("This is the Replase " + sTB.ToString()); //输出结果为 This is the Replase Hello World! /*****************************************************************/

Clear()方法:

/*****************************************************************/ //clear()方法 //清空StringBuilder内的所有内容 //沿用上个函数的StringBuilder 即 Hello World! sTB.Clear(); Console.WriteLine("This is the length " + sTB.Length); //输出结果为This is the length 0 /*****************************************************************/

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。