C#程序设计 A3.1 字符串处理(Console)

public A31Page() {
            string str = "";
            while (true)
            {
                Console.WriteLine("请输入一个长度大于3的字符串:");
                str = Console.ReadLine();
                if (str.Length <= 3)
                {
                    Console.WriteLine("字符串长度不符合要求,请重新输入!");
                    continue;
                }
                Console.WriteLine();
                break;
            }
            //字符串的长度
            Console.WriteLine("(1)字符串的长度为:{0}",str.Length);

            //出现a的位置
            int i = str.IndexOf('a');
            if (i > -1)
            {
                Console.WriteLine("(2)第一个出现a的位置是:{0}", i);
            }
            else {
                Console.WriteLine("(2)字符串中不包含字母a");
            }

            //插入helllo
            string str1 = str.Insert(3, "hello");
            Console.WriteLine("(3)插入hello后的结果:{0}",str1);

            //用me替换hello
            string str2 = str1.Replace("hello", "me");
            Console.WriteLine("(4)将hello替换me后的结果为:{0}",str2);

            //以m为分割符
            string [] arr = str2.Split('m');
            Console.WriteLine($"(5)以m为分隔符分离后的字符串有{arr.Length}个:");
            for (int j = 0; j< arr.Length; j++) {
                Console.Write(arr[j]+" ");
            }
            Console.WriteLine();

        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值