string s1="2-3";
string s2="2-4";
string s3="2-5";
string s4="6-7";
string s5="6-8";期望结果:string s="2-3-4-5";
string ss="6-7-8";也就是把首位数字一样的归类为一个字符串除了首位数字以外,其他数字不会重复。实在是没头绪万分感谢。

解决方案 »

  1.   

    用Split("-")把所有字符串分割成对应的数组
    判断数组的第一个字符串,相同的归为一组
    把一组的数组都插入到一个新数组中,每次插入前判断数组是否已存在,不存在才插入
    然后用一个for循环,把数组中的值用"-"串起来存放到字符串变量中
      

  2.   


    谢谢。求代码。还有,你写错了,是Split('-')吧。。好像是单引号
      

  3.   

    啊。是吗可我每次用双引号的时候都会报错,我这样:string[] str = "2009-09-28".Split("-");就会报错。无法从“string”转换为“char[]”所以我每次都是string[] str = "2009-09-28".Split('-'); 就没事了。。我一直用单引号。。
      

  4.   


    string [] s1 = {"2-3","2-4","2-5"};
                string s = String.Empty;
                for (int i = 0; i < s1.Length; i++)
                {
                    string[] s2 = s1[i].Split(new char[] { '-'});
                    if (s.StartsWith(s2[0]))
                    {
                        s += "-" + s2[1];
                        
                    }
                    else
                    {
                        s = s1[i];
                    }
                }类似这样?
      

  5.   


     string s1 = "2-3";
                string s2 = "2-4";
                string s3 = "2-5";
                string s4 = "6-7";
                string s5 = "6-8";
                List<string> lists = new List<string>();
                lists.Add(s1);
                lists.Add(s2);
                lists.Add(s3);
                lists.Add(s4);
                lists.Add(s5);
                SortedList<int, object> valuelist = new SortedList<int, object>();
                foreach (string temp in lists)
                {
                    string[] svalue = temp.Split('-');
                    int key = int.Parse(svalue[0]);
                    if (valuelist.ContainsKey(key))
                    {
                        List<int> templist = valuelist[key] as List<int>;
                        templist.Add(int.Parse(svalue[1]));
                    }
                    else
                    {
                        List<int> templist = new List<int>();
                        templist.Add(int.Parse(svalue[1]));
                        valuelist.Add(key, templist);
                    }
                }
                lists.Clear();
                for (int i = 0; i < valuelist.Count; i++)
                {
                    
                    int key = valuelist.Keys[i];
                    string tempstr = key.ToString();
                    List<int> templist = valuelist[key] as List<int>;
                    templist.Sort();
                    foreach (int num in templist)
                    {
                        if (num == key + 1)
                        {
                            tempstr += "-" + num.ToString();
                        }
                        else { break; }
                        ++key;
                    }
                    lists.Add(tempstr);            }
    lists就是结果
      

  6.   

              protected void Page_Load(object sender, EventArgs e)
            {
                string s1 = "2-3";
                string s2 = "2-4";
                string s3 = "2-5";
                string s4 = "6-7";
                string s5 = "6-8";            List<string> tt = new List<string>();
                tt.Add(s1);
                tt.Add(s2);
                tt.Add(s3);
                tt.Add(s4);
                tt.Add(s5);
                process(tt);
            } void process(List<string> list)
            {
               
                Dictionary<string, string> dic = new Dictionary<string, string>();
                foreach (string s in list)
                {
                    string t=s.Substring(0,1);
                    if (!dic.Keys.Contains(t))
                    {
                        dic.Add(t, s);
                    }
                    else
                    {
                        dic[t] +="-"+s;
                    }
                }
                
                foreach (string s in dic.Keys)
                {
                    Response.Write(format(dic[s]));
                    Response.Write("<br/>");
                }
            }        string  format(string s)
            {
                string result = "";
                string[] r = s.Split('-');
                List<string> t = new List<string>();
                foreach (string str in r)
                {
                    if (!t.Contains(str))
                    {
                        t.Add(str);
                        result += str + "-";
                    }
                }
                if (result.Length > 0)
                    return result.Substring(0, result.Length - 1);
                else
                    return result;
            }
      

  7.   

    首先,用Split分割字符串,把它们存入一个Dictionary里面。然后归并这个Dictionary里面的条目。
      

  8.   

    不是吧。liaoxing168 的最先出来,运行成功了,我点结贴时看到了  mbh0210 的结果看到 mbh0210 大哥写了那么多,当然我相信肯定可以,于是给了分了然后结贴后,看到了huming_h 兄弟的。。汗死大哥们都一起出来的。。
      

  9.   


    class Test
        {
            static void Main()
            {
                List<string> list = new List<string>();
                list.Add("2-3");
                list.Add("2-4");
                list.Add("2-5");
                list.Add("6-7");
                list.Add("6-8");
                list = CombineString(list);
                foreach (string s in list)
                {
                    Console.WriteLine(s);
                }
                Console.ReadLine();
            }        public static List<string> CombineString(List<string> list)
            {
                List<string> result = new List<string>();
                List<string> firstLetters = new List<string>();
                foreach (string s in list)
                {
                    string first = s.Substring(0, 1);
                    if (!firstLetters.Contains(first))
                    {
                        firstLetters.Add(first);
                    }
                }
                foreach (string s in firstLetters)
                {
                    string temp = s;
                    foreach (string i in list)
                    {
                        if (i.Substring(0, 1) == s)
                        {
                            temp += i.Substring(1);
                        }
                    }
                    result.Add(temp);
                }
                return result;
            }
        }
    写了一段代码 楼主可根据这个扩展