C#實現(xiàn)拆分字符串的示例詳解
使用正則表達式可以拆分指定的字符串。同樣地,使用字符串對象的Split方法也可以實現(xiàn)此功能。使用字符串對象的Split方法可以根據(jù)用戶選擇的拆分條件,方便地將字符串對象拆分為多個字符串。
一、使用的方法
1.使用Split(String, String)方法
在由正則表達式模式定義的位置將輸入字符串拆分為一個子字符串數(shù)組。
public static string[] Split (string input, string pattern);
參數(shù)
input String 要拆分的字符串。
pattern String 要匹配的正則表達式模式。
返回
String[] 字符串數(shù)組。
例外
ArgumentException 出現(xiàn)正則表達式分析錯誤。
ArgumentNullException input 或 pattern 為 null。
RegexMatchTimeoutException 發(fā)生超時。 有關(guān)超時的詳細信息,請參閱“備注”部分。
// 用正則表達式拆分字符串為一個子字符串數(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)詳細信息,請參閱“備注”部分。
// 將空格字符和制表 \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#操作DataGridView獲取或設(shè)置當前單元格的內(nèi)容
這篇文章介紹了C#操作DataGridView獲取或設(shè)置當前單元格的內(nèi)容,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02
WPF實現(xiàn)繪制統(tǒng)計圖(柱狀圖)的方法詳解
這篇文章主要為大家詳細介紹了如何基于WPF實現(xiàn)實現(xiàn)統(tǒng)計圖(柱狀圖)的繪制,文中的示例代碼簡潔易懂,對我們學習WPF有一定幫助,感興趣的可以了解一下2022-07-07

