请大家帮忙想想 这个怎么做:
     string num="2 3 5 6 7 8 12 15";(这个是无序的,有可能是任何正数);
     我想 插入一个数 让这个字符窜 有序:
          比如 我插入一个4 结果变成 num="2 3 4 5 6 7 8 12 15";
                 插入一个26 结果变成 num="2 3 4 5 6 7 8 12 15 26";

解决方案 »

  1.   

    string[] box=num.Split(' ');
    List<int> intBox=new List<int>();
    int i=0;
    foreach(string str in box)
    {
       intBox.Add(Convert.ToInt32(str));
    }
      int iNum=Convert.ToInt32( Console.ReadLine());
    for(int j=0;j<intBox.Length;j++)
    {
       if(intBox[j]>iNum)
       {
          intBox.Insert(iNum,j);
        }
    }
      

  2.   

    string[] box=num.Split(' '); 
    List <int> intBox=new List <int>(); 
    int i=0; 
    foreach(string str in box) 

      intBox.Add(Convert.ToInt32(str)); 

      int iNum=Convert.ToInt32( Console.ReadLine()); 
    for(int j=0;j <intBox.Length;j++) 

      if(intBox[j]>iNum) 
      { 
          intBox.Insert(iNum,j); 
          break;
        } 
    }
      

  3.   

    num先split,分割为int数组,在数组里插好后再转换为string
      

  4.   

    粗略的代码如下:
    string num = "2 3 5 6 19";
                string[] sArrayTmp = num.Split(' ');
                List<int> lTmp = new List<int>();
                foreach (string each in sArrayTmp) {
                    lTmp.Add(Convert.ToInt32(each));
                }
                lTmp.Add(4);
                lTmp.Sort();
                string sResult = "";
                foreach (int each in lTmp) {
                    sResult += each.ToString() + " ";
                }
                sResult = sResult.Substring(0, sResult.Length - 1);
      

  5.   

      额 谢谢大家了!! 问题方向问错了!!   应该是           string num="2 3 5 6 7 8 12 15";(这个是无序的,有可能是任何正数);
                      我想 插入一个数 让这个字符窜 有序:
              比如 我插入一个数 结果变成 num="1 2 3 5 6 7 8 12 15"; 返回为1;
                    再插入一个数 结果变成 num="1 2 3 4 5 6 7 8 12 15 26";返回为4;
                    再插入一个数 结果变成 num="1 2 3 4 5 6 7 8 9 12 15 26";返回为9;
      

  6.   


      额 
          string num="2 3 5 6 7 8 12 15";(这个是无序的,有可能是任何正数);
                      我想 插入一个数(任何数):
              比如 我插入一个数 结果变成 num="1 2 3 5 6 7 8 12 15"; 返回为1;
                    再插入一个数 结果变成 num="1 2 3 4 5 6 7 8 12 15 26";返回为4;
                    再插入一个数 结果变成 num="1 2 3 4 5 6 7 8 9 12 15 26";返回为9;
       
               其实插入哪个数都一样 ,我只需要 使NUM这个数有序就可以了!比如 最开始的时候 num这个数组 差1,我要的结果就是 不管插入什么数,结果都是1,第二次插入的时候
                num 这个数差4  然后结果 就应该返回4
      

  7.   

    获取数组下标
    分割字符串用正则分割
    Regex.Split(str, " ",\"(.*?)\",\"(.*?)\",\"(.*?)\",\"(.*?)\",(.*?),", RegexOptions.Compiled | RegexOptions.Singleline);if(intBox[j]>iNum) 
      { 
          intBox.Insert(iNum,j); 
        } 
    返回j
      

  8.   

      额 这样写不对啊 
      这是我的代码!
     [code] 
         protected int checkNum(int num)
            {
                List<A_Images> img = ViewState["img"] as List<A_Images>;//从viewstate 里面获取图片列表
               
                List<int> intBox = new List<int>();
                for (int i = 0; i < img.Count; i++)
                {
                    intBox.Add(img[i].Images_OrderId);//把图片顺序放到list里面
                }
                int reId = 0;
                for (int j = 0; j < intBox.Count; j++)
                {
                    if (intBox[j] > num)
                    {
                        intBox.Insert(num, j);
                        reId = j;
                        break;
                    }
                }
                return reId;
            }
    [/code]
      

  9.   

      protected int checkNum(int num)
            {
                List<A_Images> img = ViewState["img"] as List<A_Images>;//从viewstate 里面获取图片列表
               
                List<int> intBox = new List<int>();
                for (int i = 0; i < img.Count; i++)
                {
                    intBox.Add(img[i].Images_order);//存入顺序
                }
                int reId = 0;
                for (int j = 0; j < intBox.Count; j++)
                {
                    if (intBox[j] > num)
                    {
                        intBox.Insert(num, j);
                        reId = j;
                        break;
                    }
                }
                return reId;
            }
      

  10.   


                string num="2 3 5 6 7 8 12 15";
                num = num.Insert(num.Length, " 4");
                string []str1=new string []{" "};
                string[] str = num.Split(str1,StringSplitOptions.None);
                int[] myint = new int[str.Length];
                for (int j = 0; j < myint.Length; j++)
                {
                    myint[j] = int.Parse(str[j]);
                }
                Array.Sort(myint);
                string mystring = "";
                foreach (int i in myint)
                {
                    mystring = mystring +" "+ i.ToString();
                }
                Console.WriteLine(mystring);