string[] arr=a.Split(';'); int length=arr.length;//length还是Size()忘记了
string a=@"shhs;fsif;ewiewui;iweui;"; int l = a.Split(';').Length-1;
楼上这个方法倒挺好 在VB中这样才可以,C#中没有这个重载,得用Char
string a = "shhs;fsif;ewiewui;iweui;"; char c = '?'; //需要导入System.Text.RegularExpressions命名空间 int count = Regex.Matches(a,Regex.Escape(new string(c,1))).Count;
遍历记数应该比以上方法效率都高,实现也简单string a = "shhs;fsif;ewiewui;iweui;"; int count = 0; foreach (char c in a) { if (c == ';') count++; } richTextBox2.Text = count.ToString();对效率没要求那就无所谓了,否则Split和Replace效率都很低,是最不推荐的
Regex.Matches("", ";").Count
LINQ
return a.length-a.replace(";","").length
int length=arr.length;//length还是Size()忘记了
int l = a.Split(';').Length-1;
楼上这个方法倒挺好
在VB中这样才可以,C#中没有这个重载,得用Char
char c = '?';
//需要导入System.Text.RegularExpressions命名空间
int count = Regex.Matches(a,Regex.Escape(new string(c,1))).Count;
int count = 0;
foreach (char c in a)
{
if (c == ';')
count++;
}
richTextBox2.Text = count.ToString();对效率没要求那就无所谓了,否则Split和Replace效率都很低,是最不推荐的