c#字符串操作

编写控制台应用程序,要求接收一个长度大于3放到字符串,并完成下列功能。

  1. 输出字符串的长度;
  2. 输出字符串中第一个出现字母a的位置;
  3. 字符串序号从0开始编号,在字符串的第3个字符的前面插入子串hello,输出新的字符串;
  4. 将字符串hello替换为me,输出新字符串;
  5. 以字符m为分隔符,将字符串分离,并输出分离后的字符串。

3)验收/测试用例

输入1个字符串“123abcd”,显示该字符串的长度为7,第一个出现字母a的位置为3。输出123helloabcd,输出123meabcd,输出123和eabcd。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleAppstring_insert
{
    class Program
    {
        static void Main(string[] args)
        {
            {
                Console.WriteLine("请输入一个字符串(长度大于3)");
               
                string str1 = Console.ReadLine();
                if (str1.Length <= 3)
                {
                    Console.WriteLine("请输入一个字符串(长度大于3)");
                }
                else
                {
                    Console.WriteLine("字符串长度为:" + str1.Length);
                }
                char a;
                for (int i = 0; i < str1.Length; i++)
                {
                    if (str1[i] == 'a')
                    {
                        Console.WriteLine("第一次出现a的位置为");
                        Console.WriteLine(i );
                        break;
                    }
                }
                string str2 = str1.Insert(3, "hello");
                Console.WriteLine(str2);
                string str3 = str2.Replace("hello", "me");
                Console.WriteLine(str3);
                string[] str4 = str3.Split('m');
                string str5 = "";
                for (int i = 0; i < str4.Length; i++)
                {
                    if (str5 == "")
                        str5 = "" + str4[i].ToString();
                    else
                        str5 += "\n" + str4[i].ToString();
                }
                Console.WriteLine("新字符串" + str5);
                Console.ReadLine();



            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值