C#中搜索字符串中出现指定字符的次数

发布时间:2020-07-16 17:25:15 作者:1351302386
来源:网络 阅读:2466

   还在学习中,我想把每天学到的一些知识记下来,这样可以加深记忆,以后也方便自己复习吧,加油!


   

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace StringOperate

{

   /// <summary>

   /// 字符串 操作类

   /// </summary>

   class StrOperate

   {

       private static int count; //次数


       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>

       /// 定义构造函数,初始化数据

       /// </summary>

       public StrOperate(string strContent, char searchChar)

       {

           this.strContent = strContent;

           this.searchChar = searchChar;

       }


       #region //查询字符串中出现指定字符的次数


       #region 方法一

       /// <summary>

       /// 在C#中,字符串就是一个字符数组,

       /// 通过循环遍历字符数组中的每个元素,

       /// 判断得到出现指定Char的次数

       /// </summary>

       /// <returns>指定Char出现的次数(个数)</returns>

       public int SearchCharCount_1()

       {

           count = 0;

           //循环遍历string中的每个元素(字符)

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

           {

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

               if (strContent[i] == searchChar)

               {

                   //如果等于,次数加1

                   count++;

               }

           }

           //返回次数

           return count;

       }

       #endregion


       #region 方法二

       /// <summary>

       /// 通过IndexOf()函数和Substring()函数,获得次数

       /// </summary>

       /// <returns>指定Char出现的次数(个数)</returns>

       public int SearchCharCount_2()

       {

           count = 0;

           string strCont = strContent;

           while (true)

           {

               //通过IndexOf()函数 获得当前字符串中首次出现指定字符(searchChar)的位置

               int indexChar = strCont.IndexOf(searchChar);

               //如果出现的位置大于等于0,说明存在这个字符

               if (indexChar >= 0)

               {

                   //那么次数加1

                   count++;

                   //字符串更新(通过Substring()函数截取上次出现指定字符后面的字符串,以便下次搜索)

                   strCont = strCont.Substring(indexChar+1);

               }

               //否则出现的位置小于0,则说明当前字符串中不存在要搜索的字符了

               else

               {

                   //结束循环

                   break;

               }

           }

           //返回次数

           return count;

       }

       #endregion


       #region 方法三

       /// <summary>

       /// 通过Split()函数,获得次数

       /// </summary>

       /// <returns>指定Char出现的次数(个数)</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()函数和Substring()函数,获得次数

       /// </summary>

       /// <returns>指定Char出现的次数(个数)</returns>

       public int SearchCharCount_4()

       {

           count = 0;

           string strCont = strContent;

           while (true)

           {

               //通过IndexOf()函数 获得当前字符串中首次出现指定字符(searchChar)的位置

               int indexChar = strCont.LastIndexOf(searchChar);

               //如果出现的位置大于等于0,说明存在这个字符

               if (indexChar >= 0)

               {

                   //那么次数加1

                   count++;

                   //字符串更新(通过Substring()函数截取上次出现指定字符后面的字符串,以便下次搜索)

                   strCont = strCont.Substring(0,indexChar);

               }

               //否则出现的位置小于0,则说明当前字符串中不存在要搜索的字符了

               else

               {

                   //结束循环

                   break;

               }

           }

           //返回次数

           return count;

       }

       #endregion


       #region 方法五

       /// <summary>

       /// 通过indexOf()、LastIndexOf()和Substring()函数

       /// </summary>

       /// <returns>指定Char出现的次数(个数)</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()把字符串转换成一个字符数组,在判断

       /// </summary>

       /// <returns>指定Char出现的次数(个数)</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

   }

}



   

   

推荐阅读:
  1. java怎么统计字符串中字符出现的次数
  2. 使用python怎么统计字符串中字母出现的次数

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

还在学习中 肯定还有很多的不足 希望大家多提宝贵意见

上一篇:三十分钟学会物理服务器ESXI虚拟化搭建I

下一篇:Android中RecycleView如何实现指定范围的拖动效果

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》