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

C#實現(xiàn)拆分字符串的示例詳解

 更新時間:2024年02月04日 10:45:39   作者:wenchm  
這篇文章主要為大家詳細(xì)介紹了C#如何分別使用正則表達式Regex.Split方法和String.Split方法實現(xiàn)拆分字符串,有需要的小伙伴可以參考一下

使用正則表達式可以拆分指定的字符串。同樣地,使用字符串對象的Split方法也可以實現(xiàn)此功能。使用字符串對象的Split方法可以根據(jù)用戶選擇的拆分條件,方便地將字符串對象拆分為多個字符串。

一、使用的方法

1.使用Split(String, String)方法

在由正則表達式模式定義的位置將輸入字符串拆分為一個子字符串?dāng)?shù)組。

public static string[] Split (string input, string pattern);

參數(shù)

input    String 要拆分的字符串。

pattern    String 要匹配的正則表達式模式。

返回

String[] 字符串?dāng)?shù)組。

例外

ArgumentException 出現(xiàn)正則表達式分析錯誤。

ArgumentNullException input 或 pattern 為 null。

RegexMatchTimeoutException 發(fā)生超時。 有關(guān)超時的詳細(xì)信息,請參閱“備注”部分。

// 用正則表達式拆分字符串為一個子字符串?dāng)?shù)組
using System.Text.RegularExpressions;
 
namespace _086_2
{
    public class Example
    {
        public static void Main()
        {
            string input = @"01-31-2024";
            string pattern = @"(-)|(/)";
 
            foreach (string result in Regex.Split(input, pattern))
            {
                Console.WriteLine("{0}", result);
            }
        }
    }
}
// 運行結(jié)果:
/*
01
-
31
-
2024
*/

2.使用String.Split 方法

String對象的Split(Char[])方法,根據(jù)指定的分隔字符將字符串拆分為子字符串。

public string[] Split (params char[]? separator);

參數(shù)

separator    Char[] 分隔字符的數(shù)組、不包含分隔符的空數(shù)組或 null。

返回

String[] 一個數(shù)組,其元素包含此實例中的子字符串,這些子字符串由 separator 中的一個或多個字符分隔。 有關(guān)詳細(xì)信息,請參閱“備注”部分。

// 將空格字符和制表 \t 符作為分隔符
namespace _086_1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);
            string s = "Today\tI'm going to school";
            string[] subs = s.Split(' ', '\t');
 
            foreach (var sub in subs)
            {
                Console.WriteLine($"Substring: {sub}");
                //Console.WriteLine("Substring: {0}", sub);//等效語句
            }
        }
    }
}
// 運行結(jié)果:
/*
Substring: Today
Substring: I'm
Substring: going
Substring: to
Substring: school
 */

下面來分享源代碼吧:

二、源代碼

1.源碼

// 使用Split(String, String)方法拆分字符串
// 使用String對象的Split(Char[])方法拆字符串。
using System.Text.RegularExpressions;
namespace _086
{
    public partial class Form1 : Form
    {
        private GroupBox? groupBox1;
        private Button? button2;
        private Button? button1;
        private TextBox? textBox2;
        private TextBox? textBox1;
        private Label? label2;
        private Label? label1;
 
        public Form1()
        {
            InitializeComponent();
            Load += Form1_Load;
        }
        private void Form1_Load(object? sender, EventArgs e)
        {
            // 
            // label1
            // 
            label1 = new Label
            {
                AutoSize = true,
                Location = new Point(6, 23),
                Name = "label1",
                Size = new Size(68, 17),
                TabIndex = 0,
                Text = "源字符串:"
            };
            // 
            // label2
            // 
            label2 = new Label
            {
                AutoSize = true,
                Location = new Point(6, 48),
                Name = "label2",
                Size = new Size(68, 17),
                TabIndex = 1,
                Text = "子字符串:"
            };
            // 
            // textBox1
            // 
            textBox1 = new TextBox
            {
                Location = new Point(72, 17),
                Name = "textBox1",
                Size = new Size(262, 23),
                TabIndex = 2
            };
            // 
            // textBox2
            // 
            textBox2 = new TextBox
            {
                Font = new Font("Microsoft YaHei UI", 7F),
                Location = new Point(72, 48),
                Multiline = true,
                Name = "textBox2",
                Size = new Size(181, 153),
                TabIndex = 3
            };
            // 
            // button1
            // 
            button1 = new Button
            {
                Location = new Point(259, 48),
                Name = "button1",
                Size = new Size(75, 23),
                TabIndex = 4,
                Text = "拆分1",
                UseVisualStyleBackColor = true
            };
            button1.Click += Button1_Click;
            // 
            // button2
            // 
            button2 = new Button
            {
                Location = new Point(259, 74),
                Name = "button2",
                Size = new Size(75, 23),
                TabIndex = 5,
                Text = "拆分2",
                UseVisualStyleBackColor = true
            };
            button2.Click += Button2_Click;
            // 
            // groupBox1
            // 
            groupBox1 = new GroupBox
            {
                Location = new Point(12, 12),
                Name = "groupBox1",
                Size = new Size(340, 207),
                TabIndex = 0,
                TabStop = false,
                Text = "拆分字符串"
            };
            groupBox1.Controls.Add(button2);
            groupBox1.Controls.Add(button1);
            groupBox1.Controls.Add(textBox2);
            groupBox1.Controls.Add(textBox1);
            groupBox1.Controls.Add(label2);
            groupBox1.Controls.Add(label1);
            groupBox1.SuspendLayout();
 
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(364, 231);
            Controls.Add(groupBox1);
            Name = "Form1";
            StartPosition = FormStartPosition.CenterScreen;
            Text = "使用正則表達式拆分字符串";
            groupBox1.ResumeLayout(false);
            groupBox1.PerformLayout();
        }
        /// <summary>
        /// 拆分1:使用正則表達式根據(jù)數(shù)字進行拆分
        /// 遍歷拆分后的字符串集合
        /// </summary>
        private void Button1_Click(object? sender, EventArgs e)
        {
            if (textBox1!.Text != "")
            {
                textBox2!.Text = "";
                string[] str = MyRegex().Split(textBox1!.Text);
                foreach (string s in str)
                {
                    textBox2!.Text += s + Environment.NewLine;
                }
            }
            else
            {
                MessageBox.Show("源字符串不能為空", "拆分1");
            }
        }
        /// <summary>
        /// 拆分2
        /// </summary>
        private void Button2_Click(object? sender, EventArgs e)
        {
            if(textBox1!.Text != "")
            {
                textBox2!.Text = "";
                string s = textBox1!.Text;
                char[] separators = ['1', '2', '3', '4', '5', '6', '7', '8', '9'];
 
                string[] subs = s.Split(separators/*, StringSplitOptions.RemoveEmptyEntries*/);//注釋后與正則方法輸出相同結(jié)果
 
                foreach (var sub in subs)
                {
                    textBox2!.Text += sub + Environment.NewLine;
                }
            }
            else
            {
                MessageBox.Show("源字符串不能為空", "拆分2");
            }
        }
 
        [GeneratedRegex("[1-9]")]
        private static partial Regex MyRegex();
    }
}

2.生成效果

到此這篇關(guān)于C#實現(xiàn)拆分字符串的示例詳解的文章就介紹到這了,更多相關(guān)C#拆分字符串內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#智能合約部署與監(jiān)控實戰(zhàn)指南

    C#智能合約部署與監(jiān)控實戰(zhàn)指南

    本文詳細(xì)介紹了C#智能合約的部署與監(jiān)控流程,強調(diào)了有效部署、持續(xù)監(jiān)控和專業(yè)審計的重要性,文章分為部署與監(jiān)控的全流程、實戰(zhàn)案例、最佳實踐、常見問題與解決方案以及未來趨勢等五個部分,詳細(xì)介紹C#智能合約的部署與監(jiān)控的全過程,需要的朋友可以參考下
    2026-04-04
  • C#實現(xiàn)簡單飛行棋小游戲

    C#實現(xiàn)簡單飛行棋小游戲

    這篇文章主要為大家詳細(xì)介紹了C#實現(xiàn)簡單飛行棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • C# Spire.XLS for .NET輕松實現(xiàn)Excel到TXT的轉(zhuǎn)換

    C# Spire.XLS for .NET輕松實現(xiàn)Excel到TXT的轉(zhuǎn)換

    在數(shù)據(jù)處理領(lǐng)域,Excel文件以其強大的功能占據(jù)著舉足輕重的地位,本文將詳細(xì)介紹如何使用Spire.XLS for .NET幫助C#開發(fā)者輕松實現(xiàn)Excel到TXT的轉(zhuǎn)換吧
    2025-12-12
  • 如何使用C#將Tensorflow訓(xùn)練的.pb文件用在生產(chǎn)環(huán)境詳解

    如何使用C#將Tensorflow訓(xùn)練的.pb文件用在生產(chǎn)環(huán)境詳解

    這篇文章主要給大家介紹了關(guān)于如何使用C#將Tensorflow訓(xùn)練的.pb文件用在生產(chǎn)環(huán)境的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • C#實現(xiàn)子類與父類的相互轉(zhuǎn)換

    C#實現(xiàn)子類與父類的相互轉(zhuǎn)換

    這篇文章主要介紹了C#實現(xiàn)子類與父類的相互轉(zhuǎn)換,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • C# ZipArchive加壓解壓zip文件方式

    C# ZipArchive加壓解壓zip文件方式

    本文介紹了一種在內(nèi)存中創(chuàng)建zip文件并獲取其流的方法,從而避免了創(chuàng)建臨時文件和下載文件的需要,最后展示了如何解壓該zip文件
    2025-11-11
  • 基于使用BeginInvoke,EndInvoke異步調(diào)用委托的實現(xiàn)代碼

    基于使用BeginInvoke,EndInvoke異步調(diào)用委托的實現(xiàn)代碼

    本篇文章是對使用BeginInvoke,EndInvoke異步調(diào)用委托的實現(xiàn)代碼進行了分析介紹,需要的朋友參考下
    2013-05-05
  • C#使用Directoryinfo類獲得目錄信息和屬性的方法

    C#使用Directoryinfo類獲得目錄信息和屬性的方法

    這篇文章主要介紹了C#使用Directoryinfo類獲得目錄信息和屬性的方法,涉及C#操作目錄的技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • C#自定義DataGridViewColumn顯示TreeView

    C#自定義DataGridViewColumn顯示TreeView

    我們可以自定義DataGridView的DataGridViewColumn來實現(xiàn)自定義的列,下面介紹一下如何通過擴展DataGridViewColumn來實現(xiàn)一個TreeViewColumn
    2015-12-12
  • C# 獲取指定格式時間字符串的方法匯總

    C# 獲取指定格式時間字符串的方法匯總

    本文介紹了在C#中獲取指定格式時間字符串的幾種方法,包括使用`DateTime.ToString()`方法、`String.Format`或字符串插值、`DateTimeOffset`(帶時區(qū)信息),并詳細(xì)說明了常用格式模式和文化相關(guān)格式化,感興趣的朋友跟隨小編一起看看吧
    2025-11-11

最新評論

荆州市| 平利县| 监利县| 石景山区| 刚察县| 偏关县| 南京市| 民和| 徐州市| 田东县| 林口县| 曲水县| 崇左市| 祁连县| 天柱县| 富源县| 荆州市| 鄱阳县| 屏东县| 阳原县| 东莞市| 中江县| 台前县| 吴桥县| 辉县市| 湘西| 中牟县| 交口县| 广元市| 澜沧| 金沙县| 镶黄旗| 嘉善县| 那坡县| 噶尔县| 沽源县| 天台县| 弋阳县| 渝中区| 当雄县| 阿坝县|