现在的位置: 首页 > 综合 > 正文

字符串的切割和提取问题

2013年04月10日 ⁄ 综合 ⁄ 共 3575字 ⁄ 字号 评论关闭
        前一阵子,我的项目中需要用到把数据库中的一个字段根据","符号分割成多列显示在GridView控件中,折腾了半天再搞好。呵呵,看来是本人水平有限,还需要多多努力 ;)

 protected void bind_usb()  //绑定USB列表到Gridview
    {
        DataTable m_usb_Table = new DataTable();
        m_usb_Table.Columns.Add(new DataColumn("usbinfo", typeof(System.String)));
        m_usb_Table.Columns.Add(new DataColumn("usbidinfo", typeof(System.String)));
        m_usb_Table.Columns.Add(new DataColumn("usbflag", typeof(System.String)));

        ArrayList usbinfo_List = new ArrayList();
        ArrayList usbidinfo_List = new ArrayList();
        ArrayList usbflag_List = new ArrayList();

        string usbinfo_str = MyTable.Rows[0]["usbinfo"].ToString();          //从数据源 MyTable 中取得字符串
        if (usbinfo_str == "" || usbinfo_str == null) return;
        string usbidinfo_str = MyTable.Rows[0]["usbidinfo"].ToString();
        string usbflag_str = MyTable.Rows[0]["usbflag"].ToString();

        string[] usbinfo_Array = usbinfo_str.Split(',');
        foreach (string usbinfo_Sub in usbinfo_Array)
        {
            usbinfo_List.Add(usbinfo_Sub);
        }
        string[] usbidinfo_Array = usbidinfo_str.Split(',');
        foreach (string usbidinfo_Sub in usbidinfo_Array)
        {
            usbidinfo_List.Add(usbidinfo_Sub);
        }
        string[] usbflag_Array = usbflag_str.Split(',');
        foreach (string usbflag_Sub in usbflag_Array)
        {
            usbflag_List.Add(usbflag_Sub);
        }
        DataRow MyRow;
        for (int i = 0; i < usbinfo_List.Count; i++)
        {
            MyRow = m_usb_Table.NewRow();     //将 ArrayList 的值赋给 m_cpu_Table.NewRow行
            MyRow["usbinfo"] = usbinfo_List[i].ToString();
            MyRow["usbidinfo"] = usbidinfo_List[i].ToString();
            MyRow["usbflag"] = usbflag_List[i].ToString();
            m_usb_Table.Rows.Add(MyRow);
        }
        GridView usbGrid = this.Hard_DetailsView.FindControl("usbGrid") as GridView;
        //usbGrid.Visible = false;
        usbGrid.DataSource = m_usb_Table;
        usbGrid.DataBind();
    }

显示效果:

(注释:部分显示不是乱码,是日语)

        事隔多日,今天偶然看见Teracy_Joy 的blog里面也有相关代码,真是相见恨晚。也罢,自己编写的能涨能力(但没有涨工资,TMD小日本)。
引用如下:
功能:在一个文本框中输入多个数据,类似图中数据:
yyttt.JPG

要在服务器端写代码将这些字符串分开后写到一个list里面去,然后在将这个list拼写成SQL语句,弄了好久,终于有了结果,处理起来还真的有点麻烦,要是里面的数据都是整型的话可能好处理一点,现在把每一步的方法帖出来,以后好参考参考:


 1        protected void btnSel_Click(object sender, EventArgs e)
 2        {
 3            string strCode = txtManyCode.Text.Trim();
 4
 5            string[] ArrayStr = strCode.Split(',');
 6
 7            List<string> listStr = new List<string>();
 8         
 9            foreach (string str in ArrayStr)
10            {
11                listStr.Add(str);
12            }

13            DataTable dt = AcerPromotionManager.getWillDeletePromotionCode(listStr);
14
15            dgAcer.DataSource = dt;
16            dgAcer.DataBind();
17
18        }

 


 1 public static string GetStringFromStringList(List<string> list)
 2        {
 3            string strList = string.Empty;
 4            foreach (string str in list)
 5            {
 6                strList += (strList.Length == 0 ? "" : "','"+ str.ToString();
 7
 8            }

 9            return "'" + strList + "'";
10
11        }

这样就可以了,把要得到的字符串就可以提取出来了,另外还一个就是要是数据为int型的话只要调用:


 1    public static string GetStringFromIntList(List<int> list)
 2        {
 3            string strList = string.Empty;
 4
 5            foreach (int id in list)
 6            {
 7                strList += (strList.Length == 0 ? "" : ","+ id.ToString();
 8            }

 9
10            return strList;
11        }

调用方法一样的,都比较简单的。

把输入的数据中包含有单双引号的字符去掉(以及别的特殊字符)的方法:
public static string GetFormatNameForImgAlt(string str)
{
return str.Replace("'", "&rsquo;").Replace("\"", "&quot;"));
}  

发现都是湖南人,组合在一起让大家和自己都一起相互学习。多多指教!

抱歉!评论已关闭.