Net基础:统计一个字符串包含另一个字符串或字符的个数
下面总结出两种简便的方法。
方法一:
using System.Text.RegularExpressions;
Regex.Matches("abcabc","a").Count ;
方法二:
string s="abderfdegasetg";
int i=s.Split('a').Length-1;
建议:测试后方法二远大于方法一的执行速度,建议使用方法二。
下面总结出两种简便的方法。
方法一:
using System.Text.RegularExpressions;
Regex.Matches("abcabc","a").Count ;
方法二:
string s="abderfdegasetg";
int i=s.Split('a').Length-1;
建议:测试后方法二远大于方法一的执行速度,建议使用方法二。