字符串搜索的Sunday算法
public class SUNDAY
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public SUNDAY()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// TODO: 在此处添加构造函数逻辑
//
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int QfindChr(string str, string Sfind)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int str_length = 0;
int fin_length = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int find_count = 0;
int start = 0;
int moveNum = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (str.Length < Sfind.Length)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return find_count;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
str_length = str.Length;
fin_length = Sfind.Length;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
while (start + fin_length <= str_length)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
moveNum++;
bool isfind = false;//是否在这次移动中找到
string s_temp = str.Substring(start, fin_length);
if (s_temp == Sfind)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ find_count++; start = start + fin_length; isfind = true; }
if (isfind == false)//如果没找到计算下次移动位置
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int forwardPos = QfindPos(str, Sfind, start, fin_length);
start = forwardPos;
}
}
return find_count;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//找字符在字符串(不算最后一个字符)的位置(倒数)
//没找到返回fin_length,找到返回位置
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 找字符在字符串(不算最后一个字符)的位置(倒数);没找到返回str.length,找到返回位置
/// </summary>
/// <param name="str"></param>
/// <param name="find"></param>
/// <param name="pos"></param>
/// <param name="fin_length"></param>
/// <returns></returns>
public int QfindPos(string str, string find, int pos, int fin_length)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int returnPos = str.Length;
char[] Schr = str.ToCharArray();
char[] Sfin = find.ToCharArray();
if ((pos + fin_length) < str.Length)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
char chrFind = Schr[pos + fin_length];//要找的字符
if (fin_length >= 1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (find.LastIndexOf(chrFind) > -1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
returnPos = pos + fin_length - find.LastIndexOf(chrFind);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
returnPos = pos + fin_length + 1;
}
}
}
return returnPos;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
posted @
2008-05-14 18:00
雨中漫步的太阳
阅读(
1837)
评论()
编辑
收藏
举报