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

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

 

posted on 2017-06-08 13:59  选择大于努力  阅读(6636)  评论(1编辑  收藏  举报

导航