C#使用StringBuilder實現(xiàn)高效處理字符串
一、背景
字符串是不可改變的對象,字符串在創(chuàng)建以后,就不會被改變,當使用字符串對象的Replace、split或Remove等方法操作字符串時,實際上是產(chǎn)生了一個新的字符串對象,原有的字符串如果沒有被引用,將會被垃圾收集器回收。如果頻繁地使用字符串中的方法對字符串進行操作,會產(chǎn)生大量的沒有被引用的字符串對象,這會增加垃圾收集的壓力,造成系統(tǒng)資源的浪費。
二、使用StringBuilder便捷、高效地操作字符串
因為使用StringBuilder操作字符串不會產(chǎn)生新的字符串對象,在使用StringBuilder對象前首先要引用命名空間System.Text。
由于字符串的不可變性,使用StringBuilder操作字符串無疑是非常方便和有效的方法。StringBuilder對象的使用方法:
StringBuilder P_stringbuilder = new StringBuilder("abc"); //使用new關鍵字調(diào)用構(gòu)造方法創(chuàng)建對象 P_stringbuilder.Insert(2,Environment.NewLine); //調(diào)用對象的Insert方法向字符串中插入字符
建立StringBuilder對象后,可以調(diào)用操作字符串的方法,從而方便操作字符串對象適當?shù)厥褂肧tringBuilder操作字符串,會使程序運行更加高效。
三、實例
1.源碼
//按標點符號對字符串分行顯示 using System.Text; namespace _039 { public partial class Form1 : Form { private GroupBox? groupBox1; private TextBox? textBox3; private Button? button1; private TextBox? textBox1; private TextBox? textBox2; public Form1() { InitializeComponent(); Load += Form1_Load; } private void Form1_Load(object? sender, EventArgs e) { // // textBox3 // textBox3 = new TextBox { Location = new Point(12, 22), Multiline = true, Name = "textBox3", Size = new Size(310, 46), TabIndex = 6, Text = "在下面文本框中輸入字符串,并使用(,)號分隔,點擊分行顯示按鈕,根據(jù)(,)號對字符串進行分行。" }; // // button1 // button1 = new Button { Location = new Point(130, 170), Name = "button1", Size = new Size(75, 23), TabIndex = 3, Text = "分行顯示", UseVisualStyleBackColor = true }; button1.Click += Button1_Click; // // textBox1 // textBox1 = new TextBox { Location = new Point(12, 80), Multiline = true, Name = "textBox1", Size = new Size(310, 84), TabIndex = 4 }; // // textBox2 // textBox2 = new TextBox { Location = new Point(12, 200), Multiline = true, Name = "textBox2", Size = new Size(310, 84), TabIndex = 5 }; // // groupBox1 // groupBox1 = new GroupBox { Location = new Point(0, 0), Name = "groupBox1", Size = new Size(334, 74), TabIndex = 0, TabStop = false, Text = "操作說明:" }; groupBox1.Controls.Add(textBox3); groupBox1.SuspendLayout(); // // Form1 // AutoScaleDimensions = new SizeF(7F, 17F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(334, 296); Controls.Add(textBox2); Controls.Add(textBox1); Controls.Add(button1); Controls.Add(groupBox1); Name = "Form1"; StartPosition = FormStartPosition.CenterScreen; Text = "按標點符號對字符串分行"; groupBox1.ResumeLayout(false); groupBox1.PerformLayout(); } private void Button1_Click(object? sender, EventArgs e) { StringBuilder stringbuilder = new(textBox1!.Text); //創(chuàng)建字符串處理對象 for (int i = 0; i < stringbuilder.Length; i++) if (stringbuilder[i] == ',') //判斷是否出現(xiàn)(,)號 stringbuilder.Insert(++i, Environment.NewLine); //向字符串內(nèi)添加換行符 textBox2!.Text = stringbuilder.ToString(); //得到分行后的字符串 } } }
2.生成效果
到此這篇關于C#使用StringBuilder實現(xiàn)高效處理字符串的文章就介紹到這了,更多相關C# StringBuilder處理字符串內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C#9.0新特性詳解——頂級程序語句(Top-Level Programs)
這篇文章主要介紹了C#9.0新特性詳解——頂級程序語句(Top-Level Programs)的相關資料,幫助大家更好的理解和學習c#,感興趣的朋友可以了解下2020-12-12詳解如何獲取C#類中發(fā)生數(shù)據(jù)變化的屬性信息
這篇文章主要介紹了詳解如何獲取C#類中發(fā)生數(shù)據(jù)變化的屬性信息,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05