• ASP.NET利用String.Join以分隔符號來串連集合資料 [转帖]


    最近看到string有join這個用法..還不錯用..介紹給大家呀...

    有時我們會將一個集合資料以","分隔組合成一字串..例如:aaa,bbb,ccc

    利用String.Join就可以做到了...省下很多程式碼的判斷....

    asp.net(c#)

    strJoin.aspx

    1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="strJoin.aspx.cs" Inherits="strJoin" %>  
    2.   
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    4.   
    5. <html xmlns="http://www.w3.org/1999/xhtml">  
    6. <head runat="server">  
    7.     <title>strJoin</title>  
    8. </head>  
    9. <body>  
    10.     <form id="form1" runat="server">  
    11.     <div>  
    12.       
    13.         <asp:CheckBoxList ID="CheckBoxList1" runat="server"   
    14.             RepeatDirection="Horizontal">  
    15.             <asp:ListItem>F6 Team</asp:ListItem>  
    16.             <asp:ListItem>Puma</asp:ListItem>  
    17.             <asp:ListItem>Dotblogs</asp:ListItem>  
    18.             <asp:ListItem>tech‧ed 2008</asp:ListItem>  
    19.         </asp:CheckBoxList>  
    20.           
    21.   
    22.         <asp:TextBox ID="TextBox1" runat="server" Width="300px"></asp:TextBox>  
    23.         <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Join" />  
    24.       
    25.     </div>  
    26.     </form>  
    27. </body>  
    28. </html>  

    strJoin.aspx.cs

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Web;  
    4. using System.Web.UI;  
    5. using System.Web.UI.WebControls;  
    6. using System.Collections.Generic;  
    7.   
    8. public partial class strJoin : System.Web.UI.Page  
    9. {  
    10.     protected void Page_Load(object sender, EventArgs e)  
    11.     {  
    12.   
    13.     }  
    14.     protected void Button1_Click(object sender, EventArgs e)  
    15.     {  
    16.         List<string> list = new List<string>();  
    17.   
    18.         foreach (ListItem item in this.CheckBoxList1.Items)  
    19.         {  
    20.             if (item.Selected)  
    21.             {  
    22.                 list.Add(item.Text);  
    23.             }  
    24.         }  
    25.   
    26.         this.TextBox1.Text = string.Join(",",list.ToArray());  
    27.     }  
    28. }  

    執行結果:

    參考網址:

    http://msdn.microsoft.com/zh-tw/library/57a79xd0(VS.80).aspx

    转自:http://www.dotblogs.com.tw/puma/archive/2008/08/26/5200.aspx

  • 相关阅读:
    操作数据库帮助类
    VS快捷键收藏
    sqlserver 定时任务
    LayUI相关
    java20140407
    java20140406
    java20140405
    获取一个字符串在整个字符串中出现的次数
    System类
    java中的集合Collection
  • 原文地址:https://www.cnblogs.com/liangwei389/p/1402197.html
Copyright © 2020-2023  润新知