4.写一个控制台应用程序,接收一个长度大于3的字符串,完成下列功能: 1)输出字符串的长度。 2)输出字符串中第一个出现字母a的位置。 3)在字符串的第3个字符后面插入子串“hello”,输出新字符串。 4)将字符串“hello”替换为“me”,输出新字符串。 5)以字符“m”为分隔符,将字符串分离,并输出分离后的字符串。 */

 1 namespace test4
 2 {/*
 3 4.写一个控制台应用程序,接收一个长度大于3的字符串,完成下列功能:
 4  
 5 1)输出字符串的长度。
 6 2)输出字符串中第一个出现字母a的位置。
 7 3)在字符串的第3个字符后面插入子串“hello”,输出新字符串。 
 8 4)将字符串“hello”替换为“me”,输出新字符串。 
 9 5)以字符“m”为分隔符,将字符串分离,并输出分离后的字符串。 
10 */
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             string s = Console.ReadLine();
16             Console.WriteLine("字符串的长度是{0}", s.Length);
17             //for (int i = 0; i < s.Length; i++)
18             //{
19             //    if (s[i] == 'a')
20             //    {
21             //        Console.WriteLine("第一次出现a的位置是{0}", i + 1);
22             //        break;
23             //    }
24             //    Console.WriteLine("字符串中不包含a");
25             //}
26             int x = s.IndexOf('a');
27             if (x > -1)
28                 Console.WriteLine("第一个出现字母a的位置是{0}", x);
29             else
30                 Console.WriteLine("字符串中不包含{0}",'a');
31             string str1=s.Insert(3, "hello");
32             Console.WriteLine(str1);
33             string str2=str1.Replace("hello", "me"); ;
34             Console.WriteLine(str2);
35 
36             
37 
38             string[] str3 = str2.Split('m');
39             Console.WriteLine("以m为分隔符分离后的字符串有");
40             for (int j = 0; j < str3.Length; j++)
41             {
42                 Console.WriteLine(str3[j]);
43             }
44 
45             Console.ReadKey();
46 
47 
48         }
49     }
50 }

 

posted @ 2017-01-13 14:54  JefferyChang  阅读(4576)  评论(0编辑  收藏  举报