最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#使用StringBuilder實(shí)現(xiàn)高效處理字符串

 更新時(shí)間:2024年01月05日 08:50:12   作者:wenchm  
這篇文章主要為大家詳細(xì)介紹了C#如何使用StringBuilder實(shí)現(xiàn)高效處理字符串,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

一、背景

字符串是不可改變的對(duì)象,字符串在創(chuàng)建以后,就不會(huì)被改變,當(dāng)使用字符串對(duì)象的Replace、split或Remove等方法操作字符串時(shí),實(shí)際上是產(chǎn)生了一個(gè)新的字符串對(duì)象,原有的字符串如果沒(méi)有被引用,將會(huì)被垃圾收集器回收。如果頻繁地使用字符串中的方法對(duì)字符串進(jìn)行操作,會(huì)產(chǎn)生大量的沒(méi)有被引用的字符串對(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)文章

最新評(píng)論

广昌县| 定南县| 百色市| 敦化市| 焉耆| 什邡市| 赤城县| 门源| 潜江市| 伊金霍洛旗| 元谋县| 和政县| 福泉市| 博爱县| 仪征市| 和田市| 平和县| 康保县| 英山县| 北京市| 安乡县| 酒泉市| 谷城县| 宾阳县| 镇沅| 繁昌县| 门源| 林州市| 广安市| 彭山县| 罗平县| 眉山市| 宽甸| 桐城市| 宁陵县| 新化县| 庆云县| 耒阳市| 巴中市| 江阴市| 曲阳县|