字符串搜索的Sunday算法

public class SUNDAY
    
{
        
public SUNDAY()
        
{
          
//
          
// TODO: 在此处添加构造函数逻辑
          
//
        }


        
public int QfindChr(string str, string Sfind)
        
{
            
int str_length = 0;
            
int fin_length = 0;

            
int find_count = 0;
            
int start = 0;
            
int moveNum = 0;

            
if (str.Length < Sfind.Length)
            
{
                
return find_count;
            }


            str_length 
= str.Length;
            fin_length 
= Sfind.Length;

            
while (start + fin_length <= str_length)
            
{
                moveNum
++;
                
bool isfind = false;//是否在这次移动中找到
                string s_temp = str.Substring(start, fin_length);
                
if (s_temp == Sfind)
                
{ find_count++; start = start + fin_length; isfind = true; }
                
if (isfind == false)//如果没找到计算下次移动位置
                {
                    
int forwardPos = QfindPos(str, Sfind, start, fin_length);
                    start 
= forwardPos;
                }

            }

            
return find_count;
        }


        
//找字符在字符串(不算最后一个字符)的位置(倒数)
        
//没找到返回fin_length,找到返回位置
        /// <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)
        
{
            
int returnPos = str.Length;
            
char[] Schr = str.ToCharArray();
            
char[] Sfin = find.ToCharArray();
            
if ((pos + fin_length) < str.Length)
            
{
                
char chrFind = Schr[pos + fin_length];//要找的字符
                if (fin_length >= 1)
                
{
                    
if (find.LastIndexOf(chrFind) > -1)
                    
{
                        returnPos 
= pos + fin_length - find.LastIndexOf(chrFind);
                    }

                    
else
                    
{
                        returnPos 
= pos + fin_length + 1;
                    }

                }

            }

            
return returnPos;
        }

}

posted @ 2009-03-11 19:13  有安科技  阅读(437)  评论(0编辑  收藏  举报