C#使用StringBuilder實(shí)現(xiàn)高效處理字符串
一、背景
字符串是不可改變的對(duì)象,字符串在創(chuàng)建以后,就不會(huì)被改變,當(dāng)使用字符串對(duì)象的Replace、split或Remove等方法操作字符串時(shí),實(shí)際上是產(chǎn)生了一個(gè)新的字符串對(duì)象,原有的字符串如果沒有被引用,將會(huì)被垃圾收集器回收。如果頻繁地使用字符串中的方法對(duì)字符串進(jìn)行操作,會(huì)產(chǎn)生大量的沒有被引用的字符串對(duì)象,這會(huì)增加垃圾收集的壓力,造成系統(tǒng)資源的浪費(fèi)。
二、使用StringBuilder便捷、高效地操作字符串
因?yàn)槭褂肧tringBuilder操作字符串不會(huì)產(chǎn)生新的字符串對(duì)象,在使用StringBuilder對(duì)象前首先要引用命名空間System.Text。
由于字符串的不可變性,使用StringBuilder操作字符串無(wú)疑是非常方便和有效的方法。StringBuilder對(duì)象的使用方法:
StringBuilder P_stringbuilder = new StringBuilder("abc"); //使用new關(guān)鍵字調(diào)用構(gòu)造方法創(chuàng)建對(duì)象 P_stringbuilder.Insert(2,Environment.NewLine); //調(diào)用對(duì)象的Insert方法向字符串中插入字符
建立StringBuilder對(duì)象后,可以調(diào)用操作字符串的方法,從而方便操作字符串對(duì)象適當(dāng)?shù)厥褂肧tringBuilder操作字符串,會(huì)使程序運(yùn)行更加高效。
三、實(shí)例
1.源碼
//按標(biāo)點(diǎn)符號(hào)對(duì)字符串分行顯示 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 = "在下面文本框中輸入字符串,并使用(,)號(hào)分隔,點(diǎn)擊分行顯示按鈕,根據(jù)(,)號(hào)對(duì)字符串進(jìn)行分行。" }; // // 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 = "操作說(shuō)明:" }; 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 = "按標(biāo)點(diǎn)符號(hào)對(duì)字符串分行"; groupBox1.ResumeLayout(false); groupBox1.PerformLayout(); } private void Button1_Click(object? sender, EventArgs e) { StringBuilder stringbuilder = new(textBox1!.Text); //創(chuàng)建字符串處理對(duì)象 for (int i = 0; i < stringbuilder.Length; i++) if (stringbuilder[i] == ',') //判斷是否出現(xiàn)(,)號(hào) stringbuilder.Insert(++i, Environment.NewLine); //向字符串內(nèi)添加換行符 textBox2!.Text = stringbuilder.ToString(); //得到分行后的字符串 } } }
2.生成效果
到此這篇關(guān)于C#使用StringBuilder實(shí)現(xiàn)高效處理字符串的文章就介紹到這了,更多相關(guān)C# StringBuilder處理字符串內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#9.0新特性詳解——頂級(jí)程序語(yǔ)句(Top-Level Programs)
這篇文章主要介紹了C#9.0新特性詳解——頂級(jí)程序語(yǔ)句(Top-Level Programs)的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下2020-12-12C#使用Interlocked實(shí)現(xiàn)線程同步
今天小編就為大家分享一篇關(guān)于C#使用Interlocked實(shí)現(xiàn)線程同步,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10詳解如何獲取C#類中發(fā)生數(shù)據(jù)變化的屬性信息
這篇文章主要介紹了詳解如何獲取C#類中發(fā)生數(shù)據(jù)變化的屬性信息,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05