一个字符串中包含逗号个数

string str = "1,2,3,4,5,6";
怎么计算此字符串中逗号的个数?

方法1:
string str = "1,2,3,4,5,6";
int count = str.Count(ch => ch == ',');
//count=5

方法2:
str.Split(',').Length-1

方法3:
string str = "1,2,3,4,5,6";
int count = str.Length - str.Replace(",", "").Length;

方法4:
Regex.Matches(str,@",").Count

方法5:
str.ToCharArray().Where(x=x.Equals(',')).ToArray().length
原文地址:https://www.cnblogs.com/rwh871212/p/6962446.html