免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

C#中搜索字符串中出現(xiàn)指定字符的次數(shù)-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括睢寧縣網(wǎng)站建設(shè)、睢寧縣網(wǎng)站制作、睢寧縣網(wǎng)頁制作以及睢寧縣網(wǎng)絡(luò)營(yíng)銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,睢寧縣網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到睢寧縣省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(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

   }

}

   

   

創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國(guó)云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開啟,新人活動(dòng)云服務(wù)器買多久送多久。

文章名稱:C#中搜索字符串中出現(xiàn)指定字符的次數(shù)-創(chuàng)新互聯(lián)
URL鏈接:http://m.newbst.com/article24/dcepje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、網(wǎng)站改版、靜態(tài)網(wǎng)站、搜索引擎優(yōu)化虛擬主機(jī)企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司