String str = "10,20,50,60,90,21,50";
        上面一个数值,想知道,如90前面或者后面有多少个, 如何处理呀?

解决方案 »

  1.   

                String str = "10,20,50,60,90,21,50,,,";
                int count = 0;
                int pos = str.IndexOf("90,");
                if (pos > 0)
                {
                    str = str.Substring(pos+3);
                    //然后循环去取就行
                    count++;
                    pos = str.IndexOf(",");
                    while (pos >= 0)
                    {
                        str = str.Substring(pos+1);
                        count++;
                        pos = str.IndexOf(",");
                    }
                }
                else
                {
                    count = 0;
                }
      

  2.   

    一个思路string str = "10,20,50,60,90,21,50"; 
    int fNum = str.StartsWith("90")? 0 : str.Remove(str.IndexOf(",90")).Split(',').Length;
    int bNum = str.Split(',').Length - fNum -1;
    MessageBox.Show("在90前的有:" + fNum + " 个,在90后的有:" + bNum + " 个");
      

  3.   

    只是一个思路,像if (str.IndexOf("90") > -1)这样的细节楼主自己考虑下
      

  4.   

    string str = "10,20,50,60,90,21,50";
    string[] array=str.Replace(",90,","@").Split('@');
    int fCount=array[0].Split(',').Length;
    int bCount=array[1].Split(',').Length;
      

  5.   

       static void Main(string[] args)
            {
                String str = "10,20,50,60,90,21,50,,,";
                int index=str.IndexOf("90");//记录90在字符串中的下标
                int count=0;//记录90前面的,的数目
                int number = 0;//记录90后面,的数目
                string front = str.Substring(0, index);
                foreach (char a in front)
                {
                    if (a == ',')
                    {
                        count++;
                    }
                }
                string back = str.Substring(index, str.Length-index);
                foreach (char a in back)
                {
                    if (a == ',')
                        number++;
                }
                Console.WriteLine("90前的,数目:"+count);
                Console.WriteLine("90后的,数目:" + number);
                Console.ReadLine();
            }
      

  6.   

    另一个思路...string str = "10,20,50,60,90,21,50";
    List<string> list = new List<string>(str.Split(','));
    int before = list.IndexOf("90");
    int after = list.Count - before - 1;
    延伸应用...string str = "10,20,50,60,90,21,50";
    List<int> list = new List<string>(str.Split(',')).ConvertAll<int>(delegate(string s) { return int.Parse(s); });
    list.Sort();
    int before = list.IndexOf(90);//小于90的
    int after = list.Count - before - 1;//大于90的
      

  7.   

    一个思路!只需把它split到一个数组里,然后知道90在数组里的位置和数组的长度就可以搞定了
      

  8.   


    int = string.IndexOf("");强啊...又学新东西了(拜一拜)