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