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;
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(); }
另一个思路...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的
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;
}
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 + " 个");
string[] array=str.Replace(",90,","@").Split('@');
int fCount=array[0].Split(',').Length;
int bCount=array[1].Split(',').Length;
{
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();
}
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的
int = string.IndexOf("");强啊...又学新东西了(拜一拜)