プログラミング(Python、Perl、C、Go、JavaScript)、数学、読書…
開発環境
"初めてのC# 第2版"の第15章(文字列)の15.6(練習問題)を解いてみる。
練習 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("s1={0}", s1); Console.WriteLine("s2={0}", s2); Console.WriteLine("s3={0}", s3); Console.WriteLine("s4={0}", s4); Console.WriteLine("s5={0}", s5); Console.WriteLine("s6={0}", s6); Console.WriteLine("\n各文字列の長さ"); Console.WriteLine("s1: ", s1.Length); Console.WriteLine("s2: ", s2.Length); Console.WriteLine("s3: ", s3.Length); Console.WriteLine("s4: ", s4.Length); Console.WriteLine("s5: ", s5.Length); Console.WriteLine("s6: ", s6.Length); Console.WriteLine("\n各文字列の3番目の文字列"); Console.WriteLine("s1: ", s1[2]); Console.WriteLine("s2: ", s2[2]); Console.WriteLine("s3: ", s3[2]); Console.WriteLine("s4: ", s4[2]); Console.WriteLine("s5: ", s5[2]); Console.WriteLine("s6: ", s6[2]); Console.WriteLine("\n各文字列の文字「H」が含まれているかどうか"); Console.WriteLine("s1 :{0}", s1.IndexOf("H") >= 0 ? "含まれている" : "含まれていない"); Console.WriteLine("s2 :{0}", s2.IndexOf("H") >= 0 ? "含まれている" : "含まれていない"); Console.WriteLine("s3 :{0}", s3.IndexOf("H") >= 0 ? "含まれている" : "含まれていない"); Console.WriteLine("s4 :{0}", s4.IndexOf("H") >= 0 ? "含まれている" : "含まれていない"); Console.WriteLine("s5 :{0}", s5.IndexOf("H") >= 0 ? "含まれている" : "含まれていない"); Console.WriteLine("s6 :{0}", s6.IndexOf("H") >= 0 ? "含まれている" : "含まれていない"); Console.WriteLine("\ns2と同じ文字列"); Console.WriteLine("s1とs2: {0}", string.Compare(s1, s2) == 0 ? "同じ" : "違う"); Console.WriteLine("s2とs2: {0}", string.Compare(s2, s2) == 0 ? "同じ" : "違う"); Console.WriteLine("s3とs2: {0}", string.Compare(s3, s2) == 0 ? "同じ" : "違う"); Console.WriteLine("s4とs2: {0}", string.Compare(s4, s2) == 0 ? "同じ" : "違う"); Console.WriteLine("s5とs2: {0}", string.Compare(s5, s2) == 0 ? "同じ" : "違う"); Console.WriteLine("s6とs2: {0}", string.Compare(s6, s2) == 0 ? "同じ" : "違う"); Console.WriteLine("\ns2と同じ文字列(大文字/小文字を無視)"); Console.WriteLine("s1とs2: {0}", string.Compare(s1, s2, true) == 0 ? "同じ" : "違う"); Console.WriteLine("s2とs2: {0}", string.Compare(s2, s2, true) == 0 ? "同じ" : "違う"); Console.WriteLine("s3とs2: {0}", string.Compare(s3, s2, true) == 0 ? "同じ" : "違う"); Console.WriteLine("s4とs2: {0}", string.Compare(s4, s2, true) == 0 ? "同じ" : "違う"); Console.WriteLine("s5とs2: {0}", string.Compare(s5, s2, true) == 0 ? "同じ" : "違う"); Console.WriteLine("s6とs2: {0}", string.Compare(s6, s2, true) == 0 ? "同じ" : "違う"); } static void Main() { Tester t = new Tester(); t.Run(); } }
出力結果
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 各文字列の長さ s1: s2: s3: s4: s5: s6: 各文字列の3番目の文字列 s1: s2: s3: s4: s5: s6: 各文字列の文字「H」が含まれているかどうか s1 :含まれている s2 :含まれていない s3 :含まれていない s4 :含まれている s5 :含まれていない s6 :含まれていない s2と同じ文字列 s1とs2: 違う s2とs2: 同じ s3とs2: 違う s4とs2: 違う s5とs2: 違う s6とs2: 違う s2と同じ文字列(大文字/小文字を無視) s1とs2: 違う s2とs2: 同じ s3とs2: 違う s4とs2: 違う s5とs2: 同じ s6とs2: 違う 続行するには何かキーを押してください . . .
練習 15-2
using System; using System.Text; using System.Text.RegularExpressions; public class Tester { public void Run() { string s = "We hold these truths to be self-evident," + "that all men are created equal," + "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: equal 14: that 15: they 16: are 17: endowed 18: by 19: theirCreator 20: with 21: certain 22: unalienable 23: Rights 24: that 25: among 26: these 27: are 28: Life 29: Liberty 30: and 31: the 32: pursuit 33: of 34: Happiness. 続行するには何かキーを押してください . . .
0 コメント:
コメントを投稿