现在纠结的问下,关于字符串中某个子串出现的次数有没有一个好的算法,求交流
我有一个字符串,现在的想法是取出某个字符串,然后统计出这个子串在总的字符串中出现的次数。例如:“abckashdfkjhasdfhasdfaaaaasdjfkjhsakfhsahdfuuasdf";我想找到出现aa的出现次数。。求交流。。
程序代码:/******************************************************************************
* 功能: 实现对一个给定的字符串所查找的某个字符串后的到的出现次数
* 作者:yuchenli
* 创建日期:2013-8-14
* 备注:第一次好像会出现错误,希望会得到改进
* 其他:无
* ***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StringPracticeExam
{
class String
{
public int i = 0;//初始化返回参数。
public int j = 0;
/// <summary>
/// 判断字符串中给定子串的出现次数。
/// </summary>
/// <param name="Index">传入的字符串,需要找的字符串</param>
/// <param name="strNew2">在哪个字符串中寻找。</param>
/// <returns>返回出现次数</returns>
public int StringPractice(string Index,string strNew2)
{
int location= strNew2.IndexOf(Index, 0, strNew2.Length);//首先找到这个字符串所在的位置,这是我首先想到的东西,至于怎么利用这个位置。。还没想好那会
if (location == -1)//这里的-1是,假如找不到这个字符串它会返回一个-1,这是在调试中找到的。这样看来这个就是递归的终止条件了
{
return i;//直接返回当前的i,
}
else
{
i += 1;
//这里假如我的字符串是:“caabcaacbaaadf"要找的是"aa"我们希望的结果是:4,我们查找的方法是:
//逐个遍历这个字符串,每次都找到两个字符,然后和需要的字符串进行对比,假若相等,则增加,否则就找下一个,
//第一次遍历的结果是false,则比对的是"aabcaacbaaadf"很明显对比时能找到的,此时应该从第二个开始也就是第三次遍历的是:“bcaacbaaadf”
//以此类推,现在开始书写代码在另一个方法中。
#region 不规则的方法。
string StrOlder1 = strNew2.Substring(location + Index.Length, strNew2.Length - (location + Index.Length));//取子串,目的是对所给字符串得到分割,递归的查找
//string StrOlder1 = strNew2.Substring(1, strNew2.Length - 1);
if (StrOlder1.Length >= Index.Length)
{
StringPractice(Index, StrOlder1);//这里用到了递归。
}
else
{
return i;//返回参数
}
return i;//返回参数
#endregion
}
}
/// <summary>
/// 判断出现次数,这个方法实现了形如"aaa"找aa出现2次的结果
/// </summary>
/// <param name="Index">传入的字符串,需要找的字符串</param>
/// <param name="Strnew2">在哪个字符串中寻找</param>
/// <returns>返回出现次数</returns>
public int StringFind(string Index, string Strnew2)
{
string median=Strnew2;//初始化的字符串,这个字符串就是不断的查找得到的结果
#region 有了想法后的方法
if (Strnew2.Length >= Index.Length)//进行递归遍历的条件,我们不能无休止的让这个程序遍历下去。这个等号一定要有,这就是一种情况,假如,最后的字符串就是我们要找的情况
{
string tmp = Strnew2.Substring(0, Index.Length);//找出第一个遍历
if (tmp.Equals(Index))//当找出的字符串就是我们要找的,就进行返回数+1
{
j += 1;
median = median.Substring(1, median.Length - 1);//这里就是不停的一个一个的对比。其实这里还有一种情况的,还没考虑好
}
else
{
median = median.Substring(1, median.Length - 1);
//median = median.Substring(Index.Length, median.Length - Index.Length);//这个字符串是刚开始假如我们没找到,我们就减去这个字符串位置的字符串,这样考虑不妥。
}
StringFind(Index, median);//递归调用
return j;//返回参数
}
else
{
return j;//返回参数
}
#endregion
}
}
}
程序代码:/******************************************************************************
* 功能:字符串查找
* 作者:yuchenli
* 创建日期:2013-8
* 备注:貌似"ab"可以实现,但是其他不行
* **************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StringPracticeExam
{
class Program
{
static void Main(string[] args)
{
String str = new String();
//string str2="qababbwerabtyababuiopasdfghjkabl";
string str2 = "aaaaaaabddaaa";
Console.WriteLine("我们要查找的字符串是:{0}", str2);
Console.WriteLine("请输入你要查找的字符串或字符");
string str3 = Console.ReadLine();
Console.WriteLine("{0}字符串出现的次数是{1}", str3, str.StringPractice(str3, str2));
Console.WriteLine("{0}字符串出现的次数是{1}", str3, str.StringFind(str3, str2));
Console.ReadLine();
}
}
}
程序代码:string str=“abckashdfkjhasdfhasdfaaaaasdjfkjhsakfhsahdfuuasdf"; Regex regex = new Regex(@"(?isx)<div\s?class=\\?""word_wrap_518\sp_info\\?"">(?<url>(?><div[^>]*>(?<Open>)|</div>(?<-Open>)|(?:(?!</?div\b).)*)*)(?(Open)(?!))</div>", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace); MatchCollection match = regex.Matches(str); int count=match.Count;