根據這段代碼,自己編寫了一個小程序作為代碼資料參考,方便以后可以直接拿來用,不需要網上找。如果你覺得還不錯的話,就把它收藏起來吧!
1.前臺代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< html xmlns = "http://www.w3.org/1999/xhtml" > < head runat = "server" > < title >數組刪除重復值</ title > </ head > < body > < form id = "form1" runat = "server" > < div > 數組刪除前: < asp:Label ID = "lblResult1" runat = "server" ></ asp:Label > < br /> 數組刪除后: < asp:Label ID = "lblResult2" runat = "server" ></ asp:Label > </ div > </ form > </ body > </ html > |
2.后臺代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Collections; //引用 public partial class NetObjects_數組_刪除重復值 : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { string strNum = "168,145,150,148,333,888,666,168,144" ; //輸出原數組 lblResult1.Text = strNum; string [] arrNum = strNum.Split( ',' ); ArrayList al = new ArrayList(); for ( int i = 0; i < arrNum.Length; i++) { //判斷數組值是否已經存在 if (al.Contains(arrNum[i]) == false ) { al.Add(arrNum[i]); } } //把ArrayList轉換數組 arrNum = new string [al.Count]; arrNum = ( string [])al.ToArray( typeof ( string )); //輸出刪除后數組 string result = "" ; for ( int j = 0; j < arrNum.Length; j++) { if (j != 0) { result += "," ; } result += arrNum[j]; } lblResult2.Text = result; } } |
3.最終輸出效果:
以上就是關于ASP.NET數組刪除重復值的實現方法,希望對大家的學習有所幫助。