C#拆分字符串正則表達(dá)式Regex.Split和String.Split方法
前言
使用正則表達(dá)式可以拆分指定的字符串。同樣地,使用字符串對(duì)象的Split方法也可以實(shí)現(xiàn)此功能。使用字符串對(duì)象的Split方法可以根據(jù)用戶(hù)選擇的拆分條件,方便地將字符串對(duì)象拆分為多個(gè)字符串。
一、使用的方法
1.使用Split(String, String)方法
在由正則表達(dá)式模式定義的位置將輸入字符串拆分為一個(gè)子字符串?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á)式分析錯(cuò)誤。 ArgumentNullException input 或 pattern 為 null。 RegexMatchTimeoutException 發(fā)生超時(shí)。 有關(guān)超時(shí)的詳細(xì)信息,請(qǐng)參閱“備注”部分。
// 用正則表達(dá)式拆分字符串為一個(gè)子字符串?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對(duì)象的Split(Char[])方法,根據(jù)指定的分隔字符將字符串拆分為子字符串。
public string[] Split (params char[]? separator); 參數(shù) separator Char[] 分隔字符的數(shù)組、不包含分隔符的空數(shù)組或 null。 返回 String[] 一個(gè)數(shù)組,其元素包含此實(shí)例中的子字符串,這些子字符串由 separator 中的一個(gè)或多個(gè)字符分隔。 有關(guān)詳細(xì)信息,請(qǐng)參閱“備注”部分。
// 將空格字符和制表 \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ǔ)句
}
}
}
}
// 運(yùn)行結(jié)果:
/*
Substring: Today
Substring: I'm
Substring: going
Substring: to
Substring: school
*/
下面來(lái)分享源代碼吧:
二、源代碼
1.源碼
// 使用Split(String, String)方法拆分字符串
// 使用String對(duì)象的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.生成效果


總結(jié)
到此這篇關(guān)于C#拆分字符串正則表達(dá)式Regex.Split和String.Split方法的文章就介紹到這了,更多相關(guān)C#正則表達(dá)式Regex.Split和String.Split內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中Timer定時(shí)器類(lèi)的簡(jiǎn)單使用
定時(shí)器就是經(jīng)過(guò)固定時(shí)間,執(zhí)行固定任務(wù),本文主要介紹了C#中Timer定時(shí)器類(lèi)的簡(jiǎn)單使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
C#實(shí)現(xiàn)發(fā)送簡(jiǎn)單HTTP請(qǐng)求的方法
這篇文章主要介紹了C#實(shí)現(xiàn)發(fā)送簡(jiǎn)單HTTP請(qǐng)求的方法,涉及C#操作http的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04
C#實(shí)現(xiàn)六大設(shè)計(jì)原則之單一職責(zé)原則
這篇文章介紹了C#實(shí)現(xiàn)六大設(shè)計(jì)原則之單一職責(zé)原則的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02
WPF實(shí)現(xiàn)曲線(xiàn)數(shù)據(jù)展示
這篇文章將以動(dòng)數(shù)據(jù)分析為例為大家詳細(xì)介紹wpf實(shí)現(xiàn)曲線(xiàn)數(shù)據(jù)展示與函數(shù)曲線(xiàn)展示的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2024-12-12
C#9.0新特性詳解——頂級(jí)程序語(yǔ)句(Top-Level Programs)
這篇文章主要介紹了C#9.0新特性詳解——頂級(jí)程序語(yǔ)句(Top-Level Programs)的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下2020-12-12
C#日期格式字符串的相互轉(zhuǎn)換操作實(shí)例分析
這篇文章主要介紹了C#日期格式字符串的相互轉(zhuǎn)換操作,結(jié)合實(shí)例形式分析了C#日期格式字符串的相互轉(zhuǎn)換操作函數(shù)與相關(guān)使用技巧,需要的朋友可以參考下2019-08-08
WinFrom中l(wèi)abel背景透明的實(shí)現(xiàn)方法
這篇文章主要介紹了WinFrom中l(wèi)abel背景透明的實(shí)現(xiàn)方法,方法簡(jiǎn)單實(shí)用,是C#程序設(shè)計(jì)中非常實(shí)用的技巧,需要的朋友可以參考下2014-09-09

