#region --构建字符串处理 string str1 = "123AAA456AAAA789AAAAAAA1011"; string str2 = "123456789AAA23456789AAAA3456789AAAAAAA4567890"; string str3 = "123456788AAA23456789AAAA3456789AAAAAAA4567890"; string str4 = "123456788AAA0AAAA0AAAAAAA"; var strArry = (str1 + "&" + str2 + "&" + str3 + "&" + str4).Split('&'); //把字符串连接为数组 for (int i = 0; i < strArry.Length; i++) { var serachstr1 = "AAA"; var serachstr2 = "AAAA"; var serachstr3 = "AAAAAAA"; //查找3个A所在位置 int index1 = strArry[i].IndexOf(serachstr1); //查找4个A所在位置 var index2 = strArry[i].IndexOf(serachstr2, index1 + serachstr1.Length); //查找7个A所在位置 var index3 = strArry[i].IndexOf(serachstr3, index2 + serachstr2.Length); //截取字符串 var result1 = strArry[i].Substring(0,index1); var result2 = strArry[i].Substring(index1 + serachstr1.Length, index2- (index1 + serachstr1.Length)); var result3 = strArry[i].Substring(index2 + serachstr2.Length, index3-(index2 + serachstr2.Length)); var result4 = strArry[i].Substring(index3 + serachstr3.Length); } #endregion
C# 统计字符串出现的次数
string str1 = "123AAA456AAAA789AAAAAAA1011"; char[] str1Arry = str1.ToArray(); int js = 0; List<int> TempArry = new List<int>(); for (int i=0;i< str1Arry.Length;i++) { if (str1Arry[i]=='A') { js++; continue; } if (js!=0) //找到A, 不等于A时候执行 { TempArry.Add(js); js = 0; //js归0 } }