我现在得到一个string str = "15,2,4,6,1,31";现在要像里面加入一个数字假如所加上的数字要不是和上面重复的 如"11",那么 str = "[color=#0000FF]11,15,2,4,6,1,31";[/color]要是有重复的比如说是"6" 那么 str = "[color=#0000FF]6,15,2,4,1,31";[/color]也就是将有重复的数字提到前面去这个应该怎么做呢?那位告诉我一下被!

解决方案 »

  1.   

    先把字符串截取 放到一个数组中
    再用你要插入的数字 和array[i]比较 
    if(没有重复的)
    {插入 }
    else
    {做一个类似冒泡排序的 }
      

  2.   

                    string value = "6,11,1";
                    string[] arr = value.Split(',', StringSplitOptions.RemoveEmptyEntries);                List<string> lists = new List<string>(arr);
                    string newValue = "4";
                    if (lists.Contains(newValue))
                    {
                        lists.Remove(newValue);
                        lists.Insert(0, newValue);
                    }
                    else
                    {
                        lists.Insert(0, newValue);
                    }                string retVal = string.Empty;
                    foreach (string val in lists)
                    {
                        retVal += val;
                        retVal += ",";
                    }
      

  3.   

    indexOf来判断有没有?有就replace(),没有就直接加上去.....
      

  4.   

    string str1 = "6";
    string str = "15,2,4,6,1,31";
    string result = str1 + ("," + str + ",").Replace("," + str1 + ",", ",");
     Response.Write(result.Substring(0, result.Length - 1));    
      

  5.   


     public string GetProcString(string newstr , string oldstr)
            {
                if (oldstr.Length > 0) { oldstr += ","; }
                if (oldstr.IndexOf(newstr + ",") != -1)
                {
                    oldstr = oldstr.Replace(newstr + ",", "");
                }
                oldstr = newstr + "," + oldstr;
                return oldstr;
            }
      

  6.   

    oldstr = newstr + "," + oldstr;
    修改为:
    oldstr = newstr + "," + oldstr.Substring( 0,oldstr.Length-1);
      

  7.   


    public string GetProcString(string newstr , string oldstr)
            {
                if (oldstr.Length > 0) { oldstr = ","+oldstr+","; }
                if (oldstr.IndexOf(","+newstr + ",") != -1)
                {
                    oldstr = oldstr.Replace("," + newstr + ",", ",");
                }
                oldstr = newstr + "," + oldstr.Substring( 1,oldstr.Length-2);
                return oldstr;
            }那就这样处理,前后都加逗号
      

  8.   


    replace的话,就不用判断了, 找不到,自然就不会替换。:D
      

  9.   

    oldstr.Substring(1,oldstr.Length-2);这里不严谨,楼主如果能确保刚开始oldstr有数据,就不用做判断了,如果oldstr可以为空,
    那么oldstr= newstr+","+ oldstr.Substring(1,oldstr.Length-2);就修改为
    if( oldstr == ""){
     oldstr = newstr;
    }
    else
    {
       oldstr= newstr+","+ oldstr.Substring(1,oldstr.Length-2);
    }
      

  10.   


            private void button1_Click(object sender, EventArgs e)
            {
                String Str = string.Empty; 
                Str = textBox1.Text;   //这里面的值是 “1,2,3,4,5”
                if (Str.IndexOf(textBox2.Text) > 0)  //textbox2的值就是传过来的值
                {
                    Str = Str.Replace("," + textBox2.Text, "");
                }
                if (Str != textBox1.Text)
                {
                    textBox1.Text = Str + "," + textBox1.Text;
                }
            }