比如string[] a={a b c ,a a c ,a c c ,a e c};
如何获取字符串数组a中 包含b字符的元素个数
static void Main(string[] args){
string[] a={"abc","aac","acc","aec"};
int count = 0;
foreach (var item in a)
{
if(item.Contains("b")) count++;
}
Console.WriteLine(count);
}
string[] a={"abc","aac","acc","aec"};
int count = 0;
foreach (var item in a)
{
if(item.Contains("b")) count++;
}
Console.WriteLine(count);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2014-10-30
//using System.Linq;
static void Main(string[] args)
{
string[] a = { "ab", "ac", "b", "ac", "bc", "c" };
int bcnt = a.Count(x => x.Contains("b"));
Console.WriteLine(bcnt);//3
Console.ReadLine();
}
static void Main(string[] args)
{
string[] a = { "ab", "ac", "b", "ac", "bc", "c" };
int bcnt = a.Count(x => x.Contains("b"));
Console.WriteLine(bcnt);//3
Console.ReadLine();
}
第2个回答 2014-10-31
private int Function(string[] strArr, char b)
{
return (from q in strArr
where q.Contains(b)
select q).Count();
}
{
return (from q in strArr
where q.Contains(b)
select q).Count();
}
第3个回答 2014-11-05
int count = 0;
for (int i = 0; i < a.Length; i++)
if (a[i].Contains("b"))
count++;
for (int i = 0; i < a.Length; i++)
if (a[i].Contains("b"))
count++;
第4个回答 2014-10-31
int nResult = a.Where(c => c == "b").ToArray().Length;
nResult就是
nResult就是