变量str
  格式为:kc0001,kc0002,kc00003,kc0004 我想把这个变量转换为数组.!~
 然后再遍历这个数组...麻烦前辈们指点..!~

解决方案 »

  1.   

    string[] arr=str.ToString().Split(',')
    //arr[0]=kc0001
      

  2.   

    string str = "kc0001,kc0002,kc00003,kc0004";
    string[] strs = str.Split(',');
    foreach (string s in strs)
    {
       Response.Write(s);    //此时变量s便是独立的kc0001  kc0002...
    }   
      

  3.   

    var arr=str.split(",");
    foreach( var a in arr)
    {
        alert(a);
    }
      

  4.   

    试试哥的函数: public static List<string> Split(string postTag)
            {
                Regex re = new Regex("\"[\\s\\S]*?\"");
                MatchCollection mas = re.Matches(postTag);
                List<string> list = new List<string>();
                string TemPost = re.Replace(postTag, ",^#^,");            string[] splitstr = { " ", ",", "、", ",", "=", "$" };
                string[] temarray = TemPost.Split(splitstr, StringSplitOptions.RemoveEmptyEntries);
                int j = 0;
                for (int i = 0; i < temarray.Length; i++)
                {
                    if (!string.IsNullOrEmpty(temarray[i]))
                    {
                        if (temarray[i] == "^#^")
                        {
                            temarray[i] = mas[j].Value;
                            j++;
                        }
                        list.Add(temarray[i]);
                    }
                }            return list;
            }
            ///pSplitstr是你传入的自定义分隔符”比如逗号啦,句号啦“
            public static List<string> Split(string postTag, string[] pSplitstr)
            {
                Regex re = new Regex("\"[\\s\\S]*?\"");
                MatchCollection mas = re.Matches(postTag);
                List<string> list = new List<string>();
                string TemPost = re.Replace(postTag, ",^#^,");            //string[] splitstr = { " ", ",", "、", ",", "=", "$" };            string[] temarray = TemPost.Split(pSplitstr, StringSplitOptions.RemoveEmptyEntries);
                int j = 0;
                for (int i = 0; i < temarray.Length; i++)
                {
                    if (!string.IsNullOrEmpty(temarray[i]))
                    {
                        if (temarray[i] == "^#^")
                        {
                            temarray[i] = mas[j].Value;
                            j++;
                        }
                        list.Add(temarray[i]);
                    }
                }            return list;
            }
    用例:
     string[] splitChar = { "," };
     List<string> listWorkFormID = Public.Split(formIdCollection, splitChar);
      

  5.   

    写错了,js里没有foreach
    直接for就可以了var arr=str.split(",");
    for( var a in arr)
    {
        alert(a);
    }
      

  6.   


    麻烦再给解释下... 我想在数组中每个数都执行一次操作..!~
    比如数组为kc0001时,执行插入操作...
    循环后,数组变成kc0002.再执行一次插入操作..!~这个怎么写? 用for (int i=0; i<?;i++) ...用这个么?
      

  7.   

    foreach (string s in strs)
    {
       //s执行插入
    }for (int i=0; i<strs.length-1;i++)
    {
        //strs[i]执行插入
    }这两个都可以
      

  8.   


    string str = "kc0001,kc0002,kc00003,kc0004";
    string[] strs = str.Split(',');
    for (int i=0; i<strs.length;i++)
    {
      Response.Write(strs[i].Tostring()); //此时变量strs[i]便是独立的kc0001 kc0002...
      //这里执行你的insert
    }
      

  9.   

    错误 1 与“string.Split(params char[])”最匹配的重载方法具有一些无效参数 F:\msg\send.aspx.cs 141 25 F:\msg\
    错误 2 参数“1”: 无法从“string”转换为“char[]” F:\msg\send.aspx.cs 141 34 F:\msg\
    这俩错误是什么意思?5555555555
      

  10.   


                string str = "kc0001,kc0002,kc00003,kc0004";
                string[] strs = str.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries);
                foreach (string s in strs)
                {
                    //插入
                } 
      

  11.   

    谢谢各位....!~搞定了...!~感激ing...!~我先去吃饭..一会回来结贴散分..!~