计算一个字符在某一个字符串中出现的位置和次数

1、求出一个字符在某一个字符串中出现的位置和次数。

        /// <summary>
        ///
        /// </summary>
        /// <param name="str">被查找的字符串</param>
        /// <param name="strKey">查找的字符</param>

        public static void LookUpStrKey(string str, string strKey) {
            int count = 1;
            int index = 0;
            int strkeyLength = strKey.Length;
            while (true)
            {
                index = str.IndexOf(strKey, index) ;
                if (index != -1)
                {
                    Console.WriteLine("第{0}次寻找,所在字符串中的位置是{1}", count, index);
                }
                else
                {
                    break;
                }
                index += strkeyLength;
                count++;
            }
            Console.ReadKey();
        }

调用:

            string str= "咳嗽我的世界你好!咳嗽我的四阶段就是"+
                "垃圾咳嗽哈哈哈哈哈哈哈哈咳嗽搜搜哦柯索咳嗽哈哈哈哈哈哈哈哈"+
                "咳嗽哈哈哈哈哈哈哈哈咳嗽搜搜哦柯索咳嗽搜搜哦柯索";
            string strKey = "咳嗽";
            LookUpStrKey(str, strKey);

输出结果:

第1次寻找,所在字符串中的位置是0
第2次寻找,所在字符串中的位置是9
第3次寻找,所在字符串中的位置是20
第4次寻找,所在字符串中的位置是30
第5次寻找,所在字符串中的位置是37
第6次寻找,所在字符串中的位置是47
第7次寻找,所在字符串中的位置是57
第8次寻找,所在字符串中的位置是64

2、将两个集合中的元素消除重复并组合成一个新的集合,并将这个集合进行排序
            List<int> list1 = new List<int>() {1, 3, 5, 6 };
            List<int> list2 = new List<int>() { 9, 6, 7, 1 };
            for (int i = 0; i < list2.Count(); i++) {
                if (!list1.Contains(list2[i])) {
                    list1.Add(list2[i]);
                }
            }
            //list1.OrderBy(l => l);
            list1.Sort();//也是排序
            foreach (int item in list1) {
                Console.WriteLine(item);
            }
            Console.ReadKey();

3、向一个集合里面添加10个1-100的随机数,要求不能出现重复且必须是偶数。

            Random r = new Random();

            List<int> list = new List<int>();

   //方法1
            for (int i = 0; i < 10; i++)
            {

                int number = r.Next(1, 101);
                if (!list.Contains(number) && number % 2 == 0)
                    list.Add(r.Next(1, 101));
                else
                    i--;

            }

            //方法2

            while (list.Count < 10) {
                int number = r.Next(1, 101);
                if (!list.Contains(number) && number % 2 == 0) {
                    list.Add(r.Next(1, 101));
                }
            }

            foreach (int item in list)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();

4、//计算出字符串"Wellcome, to china"中每个字母出现的次数
            string str = "Wellcome, to china";
            Dictionary<char, int> dic = new Dictionary<char, int>();
            //键:字符--------------值:出现的次数
            for (int i = 0; i < str.Length; i++)
            {
                //如果是空格则跳过,只计算字母出现的次数
                if (str[i] == ' ' || str[i] == ',')
                {
                    continue;
                }
                //第一次出现则添加到字典
                if (!dic.ContainsKey(str[i]))
                    dic.Add(str[i], 1);
                else
                    dic[str[i]]++;
            }
            foreach (KeyValuePair<char, int> kv in dic)
            {
                Console.WriteLine("字母{0}出现了{1}", kv.Key, kv.Value);
            }
            Console.ReadKey();

原文地址:https://www.cnblogs.com/netlws/p/8831279.html