C#练习——去掉字符串两端的空格、统计字符出现的次数及出现的索引位置、随机生成10个1-100之间的不重复的偶数

来源:互联网 发布:用js实现下拉菜单 编辑:程序博客网 时间:2024/05/16 05:53
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace PracticeReview
{
    class Program
    {
        static void Main(string[] args)
        {
            #region  编写函数,实现类似.net中Trim()函数功能:去掉字符串两端的空格,例如将字符串"  hello      world,你  好 世界   !    "两端空格去掉,并且将其中的所有其他空格都替换成一个空格,输出结果为:"hello world,你 好 世界 !"
            string str = "  hello      world,你  好 世界   !    ";
            ////Trim()方法直接去掉字符串两边空格
            //str = str.Trim();
            //Console.WriteLine("====" + str + "===");
            string testStr = TestTrim(str);
            Console.WriteLine("====" + testStr + "===");


            //使用Split按空格分割,重载方法,StringSplitOptions.RemoveEmptyEntries返回一个不包含空字符的数组,使用这个参数要求第一个参数为字符数组
            string[] strArray = testStr.Split(new char[]{ ' ' },StringSplitOptions.RemoveEmptyEntries);
            //将分割后的字符串数组使用空格字符连接String.Join()
            string msg = String.Join(" ", strArray);
            Console.WriteLine(msg);
            Console.ReadKey();
            #endregion


            #region  随机生成10个1-100之间的不重复的偶数,填充到List<int>集合中
            //List<int> list = new List<int>();
            //将随机数种子放于循环外,可以提高效率
            //Random random = new Random();
            //while (list.Count < 10)
            //{
            //    int num = random.Next(1, 101);
            //    if (num % 2 == 0 && !list.Contains(num))
            //    {
            //        list.Add(num);
            //    }
            //}
            //for (int i = 0; i < list.Count; i++)
            //{
            //    Console.WriteLine(list[i]);
            //}
            //Console.ReadKey();
            #endregion


            #region  有如下字符串:【"患者:“大夫,我咳嗽得很重。” 大夫:“你多大年记?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?”患者:“也不咳嗽。”大夫:“那现在不咳嗽,还要等到什么时咳嗽?”"】。需求:①请统计出该字符中“咳嗽”二字的出现次数,以及每次“咳嗽”出现的索引位置。


            //string str = "患者:“大夫,我咳嗽得很重。” 大夫:“你多大年记?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?”患者:“也不咳嗽。”大夫:“那现在不咳嗽,还要等到什么时咳嗽?”";
            ////出现次数
            //int count = 0;
            ////出现的索引位置
            //int index = 0;
            //string word = "咳嗽";
            ////IndexOf()该方法返回在整个字符串中指定的字符或字符串第一次出现的索引位置,如果没有找到指定的字符或字符串则返回-1
            //while ((index = str.IndexOf(word, index)) != -1)
            //{
            //    count++;
            //    Console.WriteLine("第{0}次出现“咳嗽”,出现索引是{1}",count, index);
            //    //下次查找的时候从上一次找到“咳嗽”的索引位置处再加上“咳嗽”这个字符串的Length,从该新索引之后开始查找
            //    index += word.Length;
            //}
            //Console.ReadKey();
            #endregion
        }

        /// <summary>
        /// 编写函数,实现类似.net中Trim()函数功能:去掉字符串两端的空格
        /// </summary>
        static string TestTrim(string str)
        {
            //字符串开始索引
            int start = 0;
            //字符串结束索引
            int end = str.Length - 1;
            //假如第一个字符不是空白符;char.IsWhiteSpace()包括制表符、空格等空白符
            while (start < str.Length)
            {
                if (!char.IsWhiteSpace(str[start]))
                {
                    break;
                }
                start++;
            }
            while (end >= start)
            {
                if(!char.IsWhiteSpace(str[end]))
                {
                    break;
                }
                end--;
            }
            //Substring截取字符串
            return str.Substring(start, end - start + 1);
        }
    }
}
阅读全文
0 0
原创粉丝点击