c# 实验3 字符串处理

(1)实验目的

通过该实验,让学生掌握字符串的使用方法。

(2)实验内容

编写控制台应用程序,要求接收一个长度大于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;
namespace shixianbaogao3
{
    class program
    {
        static void Main(string[] args)
        {
            string str1;
            Console.WriteLine("请输入一个长度大于3的字符串:");
            do
            {
                str1 = Console.ReadLine();
                if (str1.Length <= 3)
                {
                    Console.WriteLine("该字符串的长度小于等于3,输入错误,请重新输入:");
                }
            } while (str1.Length <= 3);
            Console.WriteLine("该字符串长度为:"+str1.Length);
            Console.WriteLine("字符串中第一个出现字母a的位置为:" + str1.IndexOf("a"));
            string str2 = str1.Insert(3, "hello");
            Console.WriteLine("在该字符串的第3个字符的前面插入子串hello,得到的新的字符串为:" + str2);
            string str3 = str2.Replace("hello", "me");
            Console.WriteLine("将字符串hello替换为me,得到新的字符串为:" + str3);
            string[] sArray1 = str3.Split("m");
            Console.WriteLine("以字符m为分隔符,将字符串分离,得到分离后的字符串为:"+ String.Join("和", sArray1));
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_64628470/article/details/126883908
今日推荐