当前位置:   article > 正文

C#拆分字符串,正则表达式Regex.Split 方法 vs String.Split 方法

C#拆分字符串,正则表达式Regex.Split 方法 vs String.Split 方法

目录

一、使用的方法

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

2.String.Split 方法

二、源代码

1.源码

2.生成效果


        使用正则表达式可以拆分指定的字符串。同样地,使用字符串对象的Split方法也可以实现此功能。使用字符串对象的Split方法可以根据用户选择的拆分条件,方便地将字符串对象拆分为多个字符串。

一、使用的方法

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

        在由正则表达式模式定义的位置将输入字符串拆分为一个子字符串数组。

  1. public static string[] Split (string input, string pattern);
  2. 参数
  3. input String
  4. 要拆分的字符串。
  5. pattern String
  6. 要匹配的正则表达式模式。
  7. 返回
  8. String[]
  9. 字符串数组。
  10. 例外
  11. ArgumentException
  12. 出现正则表达式分析错误。
  13. ArgumentNullException
  14. input 或 pattern 为 null
  15. RegexMatchTimeoutException
  16. 发生超时。 有关超时的详细信息,请参阅“备注”部分。
  1. // 用正则表达式拆分字符串为一个子字符串数组
  2. using System.Text.RegularExpressions;
  3. namespace _086_2
  4. {
  5. public class Example
  6. {
  7. public static void Main()
  8. {
  9. string input = @"01-31-2024";
  10. string pattern = @"(-)|(/)";
  11. foreach (string result in Regex.Split(input, pattern))
  12. {
  13. Console.WriteLine("{0}", result);
  14. }
  15. }
  16. }
  17. }
  18. // 运行结果:
  19. /*
  20. 01
  21. -
  22. 31
  23. -
  24. 2024
  25. */

2.使用String.Split 方法

        String对象的Split(Char[])方法,根据指定的分隔字符将字符串拆分为子字符串。

  1. public string[] Split (params char[]? separator);
  2. 参数
  3. separator Char[]
  4. 分隔字符的数组、不包含分隔符的空数组或 null
  5. 返回
  6. String[]
  7. 一个数组,其元素包含此实例中的子字符串,这些子字符串由 separator 中的一个或多个字符分隔。 有关详细信息,请参阅“备注”部分。
  1. // 将空格字符和制表 \t 符作为分隔符
  2. namespace _086_1
  3. {
  4. internal class Program
  5. {
  6. private static void Main(string[] args)
  7. {
  8. ArgumentNullException.ThrowIfNull(args);
  9. string s = "Today\tI'm going to school";
  10. string[] subs = s.Split(' ', '\t');
  11. foreach (var sub in subs)
  12. {
  13. Console.WriteLine($"Substring: {sub}");
  14. //Console.WriteLine("Substring: {0}", sub);//等效语句
  15. }
  16. }
  17. }
  18. }
  19. // 运行结果:
  20. /*
  21. Substring: Today
  22. Substring: I'm
  23. Substring: going
  24. Substring: to
  25. Substring: school
  26. */

         下面来分享源代码吧:

二、源代码

1.源码

  1. // 使用Split(String, String)方法拆分字符串
  2. // 使用String对象的Split(Char[])方法拆字符串。
  3. using System.Text.RegularExpressions;
  4. namespace _086
  5. {
  6. public partial class Form1 : Form
  7. {
  8. private GroupBox? groupBox1;
  9. private Button? button2;
  10. private Button? button1;
  11. private TextBox? textBox2;
  12. private TextBox? textBox1;
  13. private Label? label2;
  14. private Label? label1;
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. Load += Form1_Load;
  19. }
  20. private void Form1_Load(object? sender, EventArgs e)
  21. {
  22. //
  23. // label1
  24. //
  25. label1 = new Label
  26. {
  27. AutoSize = true,
  28. Location = new Point(6, 23),
  29. Name = "label1",
  30. Size = new Size(68, 17),
  31. TabIndex = 0,
  32. Text = "源字符串:"
  33. };
  34. //
  35. // label2
  36. //
  37. label2 = new Label
  38. {
  39. AutoSize = true,
  40. Location = new Point(6, 48),
  41. Name = "label2",
  42. Size = new Size(68, 17),
  43. TabIndex = 1,
  44. Text = "子字符串:"
  45. };
  46. //
  47. // textBox1
  48. //
  49. textBox1 = new TextBox
  50. {
  51. Location = new Point(72, 17),
  52. Name = "textBox1",
  53. Size = new Size(262, 23),
  54. TabIndex = 2
  55. };
  56. //
  57. // textBox2
  58. //
  59. textBox2 = new TextBox
  60. {
  61. Font = new Font("Microsoft YaHei UI", 7F),
  62. Location = new Point(72, 48),
  63. Multiline = true,
  64. Name = "textBox2",
  65. Size = new Size(181, 153),
  66. TabIndex = 3
  67. };
  68. //
  69. // button1
  70. //
  71. button1 = new Button
  72. {
  73. Location = new Point(259, 48),
  74. Name = "button1",
  75. Size = new Size(75, 23),
  76. TabIndex = 4,
  77. Text = "拆分1",
  78. UseVisualStyleBackColor = true
  79. };
  80. button1.Click += Button1_Click;
  81. //
  82. // button2
  83. //
  84. button2 = new Button
  85. {
  86. Location = new Point(259, 74),
  87. Name = "button2",
  88. Size = new Size(75, 23),
  89. TabIndex = 5,
  90. Text = "拆分2",
  91. UseVisualStyleBackColor = true
  92. };
  93. button2.Click += Button2_Click;
  94. //
  95. // groupBox1
  96. //
  97. groupBox1 = new GroupBox
  98. {
  99. Location = new Point(12, 12),
  100. Name = "groupBox1",
  101. Size = new Size(340, 207),
  102. TabIndex = 0,
  103. TabStop = false,
  104. Text = "拆分字符串"
  105. };
  106. groupBox1.Controls.Add(button2);
  107. groupBox1.Controls.Add(button1);
  108. groupBox1.Controls.Add(textBox2);
  109. groupBox1.Controls.Add(textBox1);
  110. groupBox1.Controls.Add(label2);
  111. groupBox1.Controls.Add(label1);
  112. groupBox1.SuspendLayout();
  113. //
  114. // Form1
  115. //
  116. AutoScaleDimensions = new SizeF(7F, 17F);
  117. AutoScaleMode = AutoScaleMode.Font;
  118. ClientSize = new Size(364, 231);
  119. Controls.Add(groupBox1);
  120. Name = "Form1";
  121. StartPosition = FormStartPosition.CenterScreen;
  122. Text = "使用正则表达式拆分字符串";
  123. groupBox1.ResumeLayout(false);
  124. groupBox1.PerformLayout();
  125. }
  126. /// <summary>
  127. /// 拆分1:使用正则表达式根据数字进行拆分
  128. /// 遍历拆分后的字符串集合
  129. /// </summary>
  130. private void Button1_Click(object? sender, EventArgs e)
  131. {
  132. if (textBox1!.Text != "")
  133. {
  134. textBox2!.Text = "";
  135. string[] str = MyRegex().Split(textBox1!.Text);
  136. foreach (string s in str)
  137. {
  138. textBox2!.Text += s + Environment.NewLine;
  139. }
  140. }
  141. else
  142. {
  143. MessageBox.Show("源字符串不能为空", "拆分1");
  144. }
  145. }
  146. /// <summary>
  147. /// 拆分2
  148. /// </summary>
  149. private void Button2_Click(object? sender, EventArgs e)
  150. {
  151. if(textBox1!.Text != "")
  152. {
  153. textBox2!.Text = "";
  154. string s = textBox1!.Text;
  155. char[] separators = ['1', '2', '3', '4', '5', '6', '7', '8', '9'];
  156. string[] subs = s.Split(separators/*, StringSplitOptions.RemoveEmptyEntries*/);//注释后与正则方法输出相同结果
  157. foreach (var sub in subs)
  158. {
  159. textBox2!.Text += sub + Environment.NewLine;
  160. }
  161. }
  162. else
  163. {
  164. MessageBox.Show("源字符串不能为空", "拆分2");
  165. }
  166. }
  167. [GeneratedRegex("[1-9]")]
  168. private static partial Regex MyRegex();
  169. }
  170. }

2.生成效果

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号