開発環境
- OS: macOS High Sierra - Apple
- IDE(統合開発環境): Visual Studio for Mac
- プログラミング言語: C#
初めてのC# 第2版 (Jesse Liberty (著)、Brian MacDonald (著)、日向 俊二 (翻訳)、オライリージャパン)の15章(文字列)、15.6(練習問題)、練習15-1.を取り組んでみる。
コード
using System; namespace sample15_1 { class Program { static void Main(string[] args) { string s1 = "Hello"; string s2 = "World"; string s3 = @"Come visit us at http://www.LibertyAssociates.com"; string s4 = s1 + s2; string s5 = "world"; string s6 = String.Copy(s3); string[] strs = { s1, s2, s3, s4, s5, s6 }; Console.WriteLine("1."); foreach (var item in strs) { Console.WriteLine("{0}: {1}", item, item.Length); } Console.WriteLine("2."); foreach (var item in strs) { Console.WriteLine("{0}: {1}", item, item[2]); } Console.WriteLine("3."); foreach (var item in strs) { Console.WriteLine("{0}: {1}", item, item.IndexOf('H') >= 0); } Console.WriteLine("4."); foreach (var item in strs) { if (s2 == item) { Console.WriteLine("{0}", item); } } Console.WriteLine("5."); foreach (var item in strs) { if (s2.ToLower() == item.ToLower()) { Console.WriteLine(item); } } } } }
入出力結果(Terminal)
1. Hello: 5 World: 5 Come visit us at http://www.LibertyAssociates.com: 49 HelloWorld: 10 world: 5 Come visit us at http://www.LibertyAssociates.com: 49 2. Hello: l World: r Come visit us at http://www.LibertyAssociates.com: m HelloWorld: l world: r Come visit us at http://www.LibertyAssociates.com: m 3. Hello: True World: False Come visit us at http://www.LibertyAssociates.com: False HelloWorld: True world: False Come visit us at http://www.LibertyAssociates.com: False 4. World 5. World world Press any key to continue...
0 コメント:
コメントを投稿