前言
使用正则表达式可以拆分指定的字符串。同样地,使用字符串对象的split方法也可以实现此功能。使用字符串对象的split方法可以根据用户选择的拆分条件,方便地将字符串对象拆分为多个字符串。
一、使用的方法
1.使用split(string, string)方法
在由正则表达式模式定义的位置将输入字符串拆分为一个子字符串数组。
public static string[] split (string input, string pattern); 参数 input string 要拆分的字符串。 pattern string 要匹配的正则表达式模式。 返回 string[] 字符串数组。 例外 argumentexception 出现正则表达式分析错误。 argumentnullexception input 或 pattern 为 null。 regexmatchtimeoutexception 发生超时。 有关超时的详细信息,请参阅“备注”部分。
// 用正则表达式拆分字符串为一个子字符串数组
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);
}
}
}
}
// 运行结果:
/*
01
-
31
-
2024
*/
2.使用string.split 方法
string对象的split(char[])方法,根据指定的分隔字符将字符串拆分为子字符串。
public string[] split (params char[]? separator); 参数 separator char[] 分隔字符的数组、不包含分隔符的空数组或 null。 返回 string[] 一个数组,其元素包含此实例中的子字符串,这些子字符串由 separator 中的一个或多个字符分隔。 有关详细信息,请参阅“备注”部分。
// 将空格字符和制表 \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);//等效语句
}
}
}
}
// 运行结果:
/*
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:使用正则表达式根据数字进行拆分
/// 遍历拆分后的字符串集合
/// </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*/);//注释后与正则方法输出相同结果
foreach (var sub in subs)
{
textbox2!.text += sub + environment.newline;
}
}
else
{
messagebox.show("源字符串不能为空", "拆分2");
}
}
[generatedregex("[1-9]")]
private static partial regex myregex();
}
}
2.生成效果


总结
到此这篇关于c#拆分字符串正则表达式regex.split和string.split方法的文章就介绍到这了,更多相关c#正则表达式regex.split和string.split内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论