/// <summary>
/// 查找指定字符串在源字符串出现的次数(type--"for":遍历;"replace":替换;"split":分隔)
/// </summary>
/// <param name="sourceString">源字符串</param>
/// <param name="appointString">指定字符串</param>
/// <param name="type">查找的方式</param>
/// <returns>出现的次数(>=0:次数;-1:源字符串长度小于指定字符串;-2:源字符串或者指定字符串长度为0;-3:源字符串或者指定字符串为null)</returns>
public static int GetCountByAppointString(
string sourceString,
string appointString,
string type)
{
if (sourceString ==
null || appointString ==
null)
return -
3;
int int_count =
0;
int int_AstringLength =
appointString.Length;
int int_SstringLength =
sourceString.Length;
if (int_SstringLength <
int_AstringLength)
return -
1;
if (int_SstringLength ==
0 || int_AstringLength ==
0)
return -
2;
if (type ==
"for")
{
for (
int i =
0; i < sourceString.Length -
2; i++
)
{
if (sourceString.Substring(i, int_AstringLength) ==
appointString)
int_count++
;
}
}
else if (type ==
"replace")
{
sourceString = sourceString.Replace(appointString,
string.Empty);
int_count = (int_SstringLength - sourceString.Length) /
int_AstringLength;
}
else if (type ==
"split")
{
int_count = sourceString.Split(
new string[] { appointString }, StringSplitOptions.None).Count() -
1;
}
return int_count;
}
转载于:https://www.cnblogs.com/zichuanwahaha/p/3159899.html
相关资源:各显卡算力对照表!