权当你是要计算一个字符串中含有的子串的个数:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
string motherString = "qwertyuiopasdfghjklqwezxcqwevbnqwem";
string childString = "qwe";
var num = Count(motherString,childString);
Console.WriteLine("{0}含有的{1}个数为:{2}",motherString,childString,num);
Console.Read();
}
static int Count(string WithinString, string search)
{
if (string.IsNullOrEmpty(search))
throw new ArgumentNullException("search");
int counter = 0; int index = WithinString.IndexOf(search, 0);
while (index >= 0 && index < WithinString.Length)
{
counter++;
index = WithinString.IndexOf(search, index + search.Length);
} return counter;
}//这是一个方法,WithinString字符串,search你想查看个数的字符串,counter返回个数
}
}
希望能够帮到你,谢谢!
你写的这是啥,结果是啥,你知道我要问啥?
楼主你好,
我写的是:计算一个字符串中含有的子串的个数
结果是,能够算出来第一个字符串中含有的第二个字符串的个数
您问的是啥?