110,538
社区成员
发帖
与我相关
我的任务
分享
static int totalCount = 0;
/// <summary>
/// 获取重复次数
/// </summary>
/// <param name="value">传入的值</param>
/// <param name="key">关键字</param>
public static int GetRepetCount(string value, string key)
{
int totalValueLength = value.Trim().Length;
int keyLength = key.Length;
int startIndex = 0;
int endIndex = startIndex + keyLength;
for (int i = 0; i < totalValueLength; i++)
{
startIndex = i;
endIndex = startIndex + keyLength;
var temp = "";
if (endIndex >= totalValueLength)
{
temp = value.Substring(startIndex - 1);
}
else
{
temp = value.Substring(startIndex, keyLength);
}
if (temp == key)
{
totalCount++;
}
}
return totalCount;
}
var a = "asd asasdfg asd as zx67 asd mklo";
var b = "as";
var c = a.Split(new string[] { b }, StringSplitOptions.None).Length-1;
Response.Write(c);
a = "asd asdasdljfl lfjdsaasd ljfd";
b = "asd";
c = a.Split(new string[] { b }, StringSplitOptions.None).Length - 1;
Response.Write(c);
任何人都能看懂的....结贴给分
static int SubstringCount(string str, string substring)
{
if (str.Contains(substring))
{
string strReplaced = str.Replace(substring, "");
return (str.Length - strReplaced.Length) / substring.Length;
}
return 0;
}
public int StrCount(string a, string b)
{
if (a.Trim() != "" && b.Trim() != "")
return (a.Length - a.Replace(b, "").Length) / b.Length;
else
return 0;
}
var str = "asd asasdfg asd as zx67 asd mklo";
var substr = "as";
int Count = 0;
for(int i = 0;i <= str.Length - substr.Length;)
{
var index = str.IndexOf(substr, i);
if(index<0) break;
Count++;
i += index+1;
}
public int ReturnStringX(string a,string b)
{
int count = 0;
string n = a;
try
{
for (int i = 0; i <= a.Count(); i++)
{
Console.WriteLine(n);
if (n.Substring(0, 3).Equals(b))
{
count++;
n = n.Remove(0, 3);
}
else
{
n = n.Remove(0, 1);
}
}
}catch(Exception ex) { }
return count;
}