C#實(shí)現(xiàn)拆分字符串的示例詳解
使用正則表達(dá)式可以拆分指定的字符串。同樣地,使用字符串對(duì)象的Split方法也可以實(shí)現(xiàn)此功能。使用字符串對(duì)象的Split方法可以根據(jù)用戶選擇的拆分條件,方便地將字符串對(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.生成效果


到此這篇關(guān)于C#實(shí)現(xiàn)拆分字符串的示例詳解的文章就介紹到這了,更多相關(guān)C#拆分字符串內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
那些年,我還在學(xué)習(xí)C# 學(xué)習(xí)筆記續(xù)
那些年學(xué)習(xí)C#,就是對(duì)C#相關(guān)的一些知識(shí)有一個(gè)了解,等到要用時(shí)才不會(huì)找不到方向,比如說(shuō)擴(kuò)展方法,開(kāi)始時(shí)怎么覺(jué)得沒(méi)有用,后來(lái)了解到asp.net MVC,它可以用來(lái)擴(kuò)展Html類,比如做一個(gè)分頁(yè)的方法;所以對(duì)一門(mén)語(yǔ)言了解寬一些是沒(méi)有壞處的2012-03-03
C#連接Informix數(shù)據(jù)庫(kù)的問(wèn)題
這篇文章主要介紹了C#連接Informix數(shù)據(jù)庫(kù)的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的工作或?qū)W習(xí)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
C#操作DataGridView獲取或設(shè)置當(dāng)前單元格的內(nèi)容
這篇文章介紹了C#操作DataGridView獲取或設(shè)置當(dāng)前單元格的內(nèi)容,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
WPF實(shí)現(xiàn)繪制統(tǒng)計(jì)圖(柱狀圖)的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何基于WPF實(shí)現(xiàn)實(shí)現(xiàn)統(tǒng)計(jì)圖(柱狀圖)的繪制,文中的示例代碼簡(jiǎn)潔易懂,對(duì)我們學(xué)習(xí)WPF有一定幫助,感興趣的可以了解一下2022-07-07

