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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM