本文介绍了listbox1中的项目数是以(“A1”)和(B1)开头的? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
listBox1中总计数项的代码,知道每个:
Hi,
Code for total count items in listBox1, knows each:
label1.Text = lisBox1.Items.Count.ToString();
我需要,listBox1中的项目数以<$ c开头$ c>(A1)和(B1)
。谢谢。?
我的尝试:
之类的东西(我知道这不正确):
I need, count of items in the listBox1 what starting with ("A1")
and ("B1")
. Thanks.?
What I have tried:
something like (I know that this is not correct):
label1.Text = lisBox1.Items.Count.StartWith("A1").ToString();
label1.Text = lisBox1.Items.Count.StartWith("B1").ToString();
推荐答案
int numberOfA1 = myListBox.Items.Cast<string>().Where(s => s.StartsWith("A1")).Count();
int numberOfB1 = myListBox.Items.Cast<string>().Where(s => s.StartsWith("B1")).Count();
int cases = 0;
for (int i = 0; i <= this.listBox1.Items.Count - 1; i++)
{
if (listBox1.Items[i].ToString().ToLower().StartsWith("A1".ToLower()))
{
cases += 1;
}
}
MessageBox.Show(cases.ToString());
这篇关于listbox1中的项目数是以(“A1”)和(B1)开头的? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!