轉:http://www.dotblogs.com.tw/hung-chin/archive/2011/10/04/38717.aspx
本例是使用 Regex.Split 來以textbox1的斷行符號來做分割
然後把每一行的字串塞到陣列str裡面去
而陣列有一個Distinct方法,可以剔除重除字串
所以接著只要用foreach 把剩下的值填回去textBox1去就完成了
foreach (string strlist in str.Distinct())
{
    textBox1.Text += strlist + "\r\n";
}
以下為本例完整程式碼
| 02 | usingSystem.Collections.Generic; | 
 
| 03 | usingSystem.ComponentModel; | 
 
| 08 | usingSystem.Windows.Forms; | 
 
| 09 | usingSystem.Text.RegularExpressions; | 
 
| 12 |     publicpartialclassForm1 : Form | 
 
| 16 |             InitializeComponent(); | 
 
| 18 |         privatevoidbutton1_Click(objectsender, EventArgs e) | 
 
| 20 |             string[] str = Regex.Split(textBox1.Text, "\r\n"); | 
 
| 22 |             foreach(stringstrlist instr.Distinct()) | 
 
| 24 |                 textBox1.Text += strlist + "\r\n"; | 
 
 
 
 
 
 
沒有留言:
張貼留言