溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#中搜索字符串中出現(xiàn)指定字符的次數(shù)

發(fā)布時(shí)間:2020-07-16 17:25:15 來源:網(wǎng)絡(luò) 閱讀:2484 作者:1351302386 欄目:編程語言

   還在學(xué)習(xí)中,我想把每天學(xué)到的一些知識(shí)記下來,這樣可以加深記憶,以后也方便自己復(fù)習(xí)吧,加油!


   

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace StringOperate

{

   /// <summary>

   /// 字符串 操作類

   /// </summary>

   class StrOperate

   {

       private static int count; //次數(shù)


       public static int Count

       {

           get { return StrOperate.count; }

           set { StrOperate.count = value; }

       }


       private string strContent; //字符串


       public string StrContent

       {

           get { return strContent; }

           set { strContent = value; }

       }


       private char searchChar; //需要查詢的字符


       public char SearchChar

       {

           get { return searchChar; }

           set { searchChar = value; }

       }


       /// <summary>

       /// 定義構(gòu)造函數(shù),初始化數(shù)據(jù)

       /// </summary>

       public StrOperate(string strContent, char searchChar)

       {

           this.strContent = strContent;

           this.searchChar = searchChar;

       }


       #region //查詢字符串中出現(xiàn)指定字符的次數(shù)


       #region 方法一

       /// <summary>

       /// 在C#中,字符串就是一個(gè)字符數(shù)組,

       /// 通過循環(huán)遍歷字符數(shù)組中的每個(gè)元素,

       /// 判斷得到出現(xiàn)指定Char的次數(shù)

       /// </summary>

       /// <returns>指定Char出現(xiàn)的次數(shù)(個(gè)數(shù))</returns>

       public int SearchCharCount_1()

       {

           count = 0;

           //循環(huán)遍歷string中的每個(gè)元素(字符)

           for (int i = 0; i < strContent.Length; i++)

           {

               //判斷是否等于指定的字符

               if (strContent[i] == searchChar)

               {

                   //如果等于,次數(shù)加1

                   count++;

               }

           }

           //返回次數(shù)

           return count;

       }

       #endregion


       #region 方法二

       /// <summary>

       /// 通過IndexOf()函數(shù)和Substring()函數(shù),獲得次數(shù)

       /// </summary>

       /// <returns>指定Char出現(xiàn)的次數(shù)(個(gè)數(shù))</returns>

       public int SearchCharCount_2()

       {

           count = 0;

           string strCont = strContent;

           while (true)

           {

               //通過IndexOf()函數(shù) 獲得當(dāng)前字符串中首次出現(xiàn)指定字符(searchChar)的位置

               int indexChar = strCont.IndexOf(searchChar);

               //如果出現(xiàn)的位置大于等于0,說明存在這個(gè)字符

               if (indexChar >= 0)

               {

                   //那么次數(shù)加1

                   count++;

                   //字符串更新(通過Substring()函數(shù)截取上次出現(xiàn)指定字符后面的字符串,以便下次搜索)

                   strCont = strCont.Substring(indexChar+1);

               }

               //否則出現(xiàn)的位置小于0,則說明當(dāng)前字符串中不存在要搜索的字符了

               else

               {

                   //結(jié)束循環(huán)

                   break;

               }

           }

           //返回次數(shù)

           return count;

       }

       #endregion


       #region 方法三

       /// <summary>

       /// 通過Split()函數(shù),獲得次數(shù)

       /// </summary>

       /// <returns>指定Char出現(xiàn)的次數(shù)(個(gè)數(shù))</returns>

       public int SearchCharCount_3()

       {

           count = 0;

           //按照指定的字符對(duì)字符串進(jìn)行拆分(那么有多少個(gè)字符就會(huì)拆分多少次)

           string[] strs = strContent.Split(searchChar);

           //for (int i = 0; i < strs.Length; i++)

           //{

           //    Console.WriteLine("-" + strs[i] + "-");

           //}

           count = strs.Length - 1;

           Console.WriteLine("Lenght = " + strs.Length);

           return count;

       }

       #endregion


       #region 方法四

       /// <summary>

       /// 通過LastIndexOf()函數(shù)和Substring()函數(shù),獲得次數(shù)

       /// </summary>

       /// <returns>指定Char出現(xiàn)的次數(shù)(個(gè)數(shù))</returns>

       public int SearchCharCount_4()

       {

           count = 0;

           string strCont = strContent;

           while (true)

           {

               //通過IndexOf()函數(shù) 獲得當(dāng)前字符串中首次出現(xiàn)指定字符(searchChar)的位置

               int indexChar = strCont.LastIndexOf(searchChar);

               //如果出現(xiàn)的位置大于等于0,說明存在這個(gè)字符

               if (indexChar >= 0)

               {

                   //那么次數(shù)加1

                   count++;

                   //字符串更新(通過Substring()函數(shù)截取上次出現(xiàn)指定字符后面的字符串,以便下次搜索)

                   strCont = strCont.Substring(0,indexChar);

               }

               //否則出現(xiàn)的位置小于0,則說明當(dāng)前字符串中不存在要搜索的字符了

               else

               {

                   //結(jié)束循環(huán)

                   break;

               }

           }

           //返回次數(shù)

           return count;

       }

       #endregion


       #region 方法五

       /// <summary>

       /// 通過indexOf()、LastIndexOf()和Substring()函數(shù)

       /// </summary>

       /// <returns>指定Char出現(xiàn)的次數(shù)(個(gè)數(shù))</returns>

       public int SearchCharCount_5()

       {

           count = 0;

           string strCont = strContent;

           while (true)

           {

               int indexof = strCont.IndexOf(searchChar);

               int lastIndexof = strCont.LastIndexOf(searchChar);

               if (indexof != lastIndexof)

               {

                   count += 2;

                   strCont = strCont.Substring(indexof + 1, lastIndexof - 1 - indexof);

                   //bacdefa

                   //0123456

               }

               else

               {

                   break;

               }

           }

           return count;

       }

       #endregion


       #region 方法六

       /// <summary>

       /// 使用string.ToArray()把字符串轉(zhuǎn)換成一個(gè)字符數(shù)組,在判斷

       /// </summary>

       /// <returns>指定Char出現(xiàn)的次數(shù)(個(gè)數(shù))</returns>

       public int SearchCharCount_6()

       {

           count = 0;

           char[] chars = strContent.ToArray();

           for (int i = 0; i < chars.Length; i++)

           {

               if(chars[i] == searchChar)

               {

                   count++;

               }

           }

           return count;

       }

       #endregion


       #endregion

   }

}



   

   

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI