编写控制台应用程序,要求接收一个长度大于3放到字符串,并完成下列功能。
- 输出字符串的长度;
- 输出字符串中第一个出现字母a的位置;
- 字符串序号从0开始编号,在字符串的第3个字符的前面插入子串hello,输出新的字符串;
- 将字符串hello替换为me,输出新字符串;
- 以字符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();
}
}
}
}