内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

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

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

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

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

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

       /// </summary>

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

       public int SearchCharCount_1()

       {

           count = 0;

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

           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ù)(個數(shù))</returns>

       public int SearchCharCount_2()

       {

           count = 0;

           string strCont = strContent;

           while (true)

           {

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

               int indexChar = strCont.IndexOf(searchChar);

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

               if (indexChar >= 0)

               {

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

                   count++;

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

                   strCont = strCont.Substring(indexChar+1);

               }

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

               else

               {

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

                   break;

               }

           }

           //返回次數(shù)

           return count;

       }

       #endregion

       #region 方法三

       /// <summary>

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

       /// </summary>

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

       public int SearchCharCount_3()

       {

           count = 0;

           //按照指定的字符對字符串進行拆分(那么有多少個字符就會拆分多少次)

           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ù)(個數(shù))</returns>

       public int SearchCharCount_4()

       {

           count = 0;

           string strCont = strContent;

           while (true)

           {

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

               int indexChar = strCont.LastIndexOf(searchChar);

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

               if (indexChar >= 0)

               {

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

                   count++;

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

                   strCont = strCont.Substring(0,indexChar);

               }

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

               else

               {

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

                   break;

               }

           }

           //返回次數(shù)

           return count;

       }

       #endregion

       #region 方法五

       /// <summary>

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

       /// </summary>

       /// <returns>指定Char出現(xiàn)的次數(shù)(個數(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)換成一個字符數(shù)組,在判斷

       /// </summary>

       /// <returns>指定Char出現(xiàn)的次數(shù)(個數(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è)提供香港、美國云服務器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡助力業(yè)務部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準確進行流量調(diào)度,確保服務器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務器買多久送多久。

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

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

廣告

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

成都app開發(fā)公司
普格县| 鲁甸县| 抚松县| 都匀市| 保亭| 西充县| 葫芦岛市| 城市| 徐州市| 马龙县| 寿宁县| 夹江县| 静宁县| 万荣县| 始兴县| 小金县| 蚌埠市| 会理县| 浦县| 拜城县| 荔波县| 岑溪市| 台州市| 永康市| 萍乡市| 东港市| 彰化市| 锦屏县| 阳春市| 饶阳县| 宁陕县| 安平县| 曲周县| 泗洪县| 安化县| 唐山市| 宁蒗| 宜昌市| 肃宁县| 瑞昌市| 河北省|