字串搜尋的Sunday演算法

來源:互聯網
上載者:User

字串搜尋的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;
        }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.