字串分割Regex.Split

字串分割Regex.Split

Regex.Split 方法類似 String.Split 方法,只是 Split 方法以規則運算式所決定的分隔符號

來分隔字串,而不是使用一組字元。

可參考MSDN

 


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Split
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = "5,10,15,20,-1,-2";

        }

        private void button1_Click(object sender, EventArgs e)
        {
          string [] x=  Regex.Split(label1.Text, ",");
          for (int i = 0; i < x.Length; i++)
          {
              listBox1.Items.Add(x[i]);
          }           
        }
    }
}


 


如有錯誤 歡迎指正