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

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 的地方全部替换成为 newValue
Clear()方法 清空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
            
            /*****************************************************************/

你可能感兴趣的:(C#,Unity)