欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

一、使用的方法

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

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

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

參數(shù)

input    String 要拆分的字符串。

pattern    String 要匹配的正則表達(dá)式模式。

返回

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

例外

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

ArgumentNullException input 或 pattern 為 null。

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

// 用正則表達(dá)式拆分字符串為一個子字符串?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);
            }
        }
    }
}
// 運(yùn)行結(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);//等效語句
            }
        }
    }
}
// 運(yùn)行結(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 = "使用正則表達(dá)式拆分字符串";
            groupBox1.ResumeLayout(false);
            groupBox1.PerformLayout();
        }
        /// <summary>
        /// 拆分1:使用正則表達(dá)式根據(jù)數(shù)字進(jìn)行拆分
        /// 遍歷拆分后的字符串集合
        /// </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#中的字符串拼接@ $

    詳解C#中的字符串拼接@ $

    這篇文章主要介紹了C#中的字符串拼接@,$的相關(guān)知識,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • 如何在c#中使用Zlib壓縮與解壓

    如何在c#中使用Zlib壓縮與解壓

    這篇文章主要介紹了如何在c#中使用Zlib壓縮與解壓,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-04-04
  • 那些年,我還在學(xué)習(xí)C# 學(xué)習(xí)筆記續(xù)

    那些年,我還在學(xué)習(xí)C# 學(xué)習(xí)筆記續(xù)

    那些年學(xué)習(xí)C#,就是對C#相關(guān)的一些知識有一個了解,等到要用時才不會找不到方向,比如說擴(kuò)展方法,開始時怎么覺得沒有用,后來了解到asp.net MVC,它可以用來擴(kuò)展Html類,比如做一個分頁的方法;所以對一門語言了解寬一些是沒有壞處的
    2012-03-03
  • C#連接Informix數(shù)據(jù)庫的問題

    C#連接Informix數(shù)據(jù)庫的問題

    這篇文章主要介紹了C#連接Informix數(shù)據(jù)庫的問題,本文給大家介紹的非常詳細(xì),對大家的工作或?qū)W習(xí)具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • .net與javascript腳本的交互方法總結(jié)

    .net與javascript腳本的交互方法總結(jié)

    這篇文章主要介紹了.net與javascript腳本的交互方法,實例講述了.net訪問javascript的方法與javascript訪問.net的方法,非常具有實用價值,需要的朋友可以參考下
    2014-10-10
  • C#路徑,文件,目錄及IO常見操作匯總

    C#路徑,文件,目錄及IO常見操作匯總

    這篇文章主要介紹了C#路徑,文件,目錄及IO常見操作,較為詳細(xì)的分析并匯總了C#關(guān)于路徑,文件,目錄及IO常見操作,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • C#操作DataGridView獲取或設(shè)置當(dāng)前單元格的內(nèi)容

    C#操作DataGridView獲取或設(shè)置當(dāng)前單元格的內(nèi)容

    這篇文章介紹了C#操作DataGridView獲取或設(shè)置當(dāng)前單元格的內(nèi)容,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02
  • C#中分部方法和分部類分析

    C#中分部方法和分部類分析

    這篇文章主要介紹了C#中分部方法和分部類基本用法,并且較為詳細(xì)的分析了分部方法和分部類使用時的注意事項,需要的朋友可以參考下
    2014-11-11
  • C#異步編程由淺入深(三)之詳解Awaiter

    C#異步編程由淺入深(三)之詳解Awaiter

    這篇文章主要介紹了C#異步編程由淺入深(三)之詳解Awaiter,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • WPF實現(xiàn)繪制統(tǒng)計圖(柱狀圖)的方法詳解

    WPF實現(xiàn)繪制統(tǒng)計圖(柱狀圖)的方法詳解

    這篇文章主要為大家詳細(xì)介紹了如何基于WPF實現(xiàn)實現(xiàn)統(tǒng)計圖(柱狀圖)的繪制,文中的示例代碼簡潔易懂,對我們學(xué)習(xí)WPF有一定幫助,感興趣的可以了解一下
    2022-07-07

最新評論