プログラミング(Python、Perl、C、Go、JavaScript)、数学、読書…
開発環境
"初めてのC# 第2版"の第15章(文字列)の15.6(練習問題)を解いてみる。
練習 15-1
using System; class Tester { void Run() { string s1 = "Hello"; string s2 = "World"; string s3 = @"Comevisit us at http://www.LibertyAssociates.com"; string s4 = s1 + s2; string s5 = "world"; string s6 = string.Copy(s3); Console.WriteLine("各文字列の長さ"); Console.WriteLine("{0}: {1}", s1, s1.Length); Console.WriteLine("{0}: {1}", s2, s2.Length); Console.WriteLine("{0}: {1}", s3, s3.Length); Console.WriteLine("{0}: {1}", s4, s4.Length); Console.WriteLine("{0}: {1}", s5, s5.Length); Console.WriteLine("{0}: {1}", s6, s6.Length); Console.WriteLine("\n各文字列の3番目の文字"); Console.WriteLine("{0}: {1}", s1, s1[2]); Console.WriteLine("{0}: {1}", s2, s2[2]); Console.WriteLine("{0}: {1}", s3, s3[2]); Console.WriteLine("{0}: {1}", s4, s4[2]); Console.WriteLine("{0}: {1}", s5, s5[2]); Console.WriteLine("{0}: {1}", s6, s6[2]); Console.WriteLine("\n各文字列に「H」が含まれているか"); Console.WriteLine( "{0}: {1}", s1, (s1.IndexOf("H") >= 0) ? "含まれている" : "含まれていない"); Console.WriteLine( "{0}: {1}", s2, (s2.IndexOf("H") >= 0) ? "含まれている" : "含まれていない"); Console.WriteLine( "{0}: {1}", s3, (s3.IndexOf("H") >= 0) ? "含まれている" : "含まれていない"); Console.WriteLine( "{0}: {1}", s4, (s4.IndexOf("H") >= 0) ? "含まれている" : "含まれていない"); Console.WriteLine( "{0}: {1}", s5, (s5.IndexOf("H") >= 0) ? "含まれている" : "含まれていない"); Console.WriteLine( "{0}: {1}", s6, (s6.IndexOf("H") >= 0) ? "含まれている" : "含まれていない"); Console.WriteLine("\n各文字列は文字列「{0}」と同じか", s2); Console.WriteLine( "{0}: {1}", s1, (string.Compare(s1, s2) == 0) ? "同じ" : "異なる"); Console.WriteLine( "{0}: {1}", s3, (string.Compare(s3, s2) == 0) ? "同じ" : "異なる"); Console.WriteLine( "{0}: {1}", s4, (string.Compare(s4, s2) == 0) ? "同じ" : "異なる"); Console.WriteLine( "{0}: {1}", s5, (string.Compare(s5, s2) == 0) ? "同じ" : "異なる"); Console.WriteLine( "{0}: {1}", s6, (string.Compare(s6, s2) == 0) ? "同じ" : "異なる"); Console.WriteLine( "\n各文字列は文字列「{0}」と同じか(上記と異なり大文字/小文字は無視する)", s2); Console.WriteLine( "{0}: {1}", s1, (string.Compare(s1, s2, true) == 0) ? "同じ" : "異なる"); Console.WriteLine( "{0}: {1}", s3, (string.Compare(s3, s2, true) == 0) ? "同じ" : "異なる"); Console.WriteLine( "{0}: {1}", s4, (string.Compare(s4, s2, true) == 0) ? "同じ" : "異なる"); Console.WriteLine( "{0}: {1}", s5, (string.Compare(s5, s2, true) == 0) ? "同じ" : "異なる"); Console.WriteLine( "{0}: {1}", s6, (string.Compare(s6, s2, true) == 0) ? "同じ" : "異なる"); } static void Main() { Tester t = new Tester(); t.Run(); } }
出力結果
各文字列の長さ Hello: 5 World: 5 Comevisit us at http://www.LibertyAssociates.com: 48 HelloWorld: 10 world: 5 Comevisit us at http://www.LibertyAssociates.com: 48 各文字列の3番目の文字 Hello: l World: r Comevisit us at http://www.LibertyAssociates.com: m HelloWorld: l world: r Comevisit us at http://www.LibertyAssociates.com: m 各文字列に「H」が含まれているか Hello: 含まれている World: 含まれていない Comevisit us at http://www.LibertyAssociates.com: 含まれていない HelloWorld: 含まれている world: 含まれていない Comevisit us at http://www.LibertyAssociates.com: 含まれていない 各文字列は文字列「World」と同じか Hello: 異なる Comevisit us at http://www.LibertyAssociates.com: 異なる HelloWorld: 異なる world: 異なる Comevisit us at http://www.LibertyAssociates.com: 異なる 各文字列は文字列「World」と同じか(上記と異なり大文字/小文字は無視する) Hello: 異なる Comevisit us at http://www.LibertyAssociates.com: 異なる HelloWorld: 異なる world: 同じ Comevisit us at http://www.LibertyAssociates.com: 異なる 続行するには何かキーを押してください . . .
練習 15-2
using System; using System.Text; using System.Text.RegularExpressions; class Tester { public void Run() { string s = "We hold these truths to be self-evident," + "that all men are created equals," + "that they are endowed by their Creator with certain unalienable Rights," + "that among these are Life," + "Liberty and the pursuit of Happiness."; Regex theRegex = new Regex(" |, |,"); StringBuilder sBuilder = new StringBuilder(); int id = 1; foreach (string subString in theRegex.Split(s)) { sBuilder.AppendFormat("{0}: {1}\n", id++, subString); } Console.WriteLine("{0}", sBuilder); } static void Main() { Tester t = new Tester(); t.Run(); } }
1: We 2: hold 3: these 4: truths 5: to 6: be 7: self-evident 8: that 9: all 10: men 11: are 12: created 13: equals 14: that 15: they 16: are 17: endowed 18: by 19: their 20: Creator 21: with 22: certain 23: unalienable 24: Rights 25: that 26: among 27: these 28: are 29: Life 30: Liberty 31: and 32: the 33: pursuit 34: of 35: Happiness. 続行するには何かキーを押してください . . .
0 コメント:
コメントを投稿