開発環境
- Microsoft Windows 8 Pro 64bit 日本語 (OS)
- Microsoft Visual Studio Express 2012 for Windows Desktop (IDE)
- プログラミング言語: C#
『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487311-294-7)の 第15章(文字列)14.6(練習問題)練習15-1-3を解いてみる。
その他参考書籍
練習15-1-3.
コード
using System; class Tester { public void Run() { string str1 = "Hello", str2 = "World", str3 = @"Come visited us at http://www.LibertyAssociates.com", str41 = str1 + str2, str42 = string.Concat(str1, str2), str5 = "world", str61 = string.Copy(str3), str62 = str3; string[] strs = { str1, str2, str3, str41, str42, str5, str61, str62 }; Console.WriteLine("各文字列に文字「H」が含まれているかどうか"); foreach (string str in strs) { string msg = str.IndexOf("H") >= 0 ? "含まれている" : "含まれていない"; Console.WriteLine("{0}: {1}", str, msg); } } static void Main() { Tester t = new Tester(); t.Run(); } }
入出力結果(Console Window)
各文字列に文字「H」が含まれているかどうか Hello: 含まれている World: 含まれていない Come visited us at http://www.LibertyAssociates.com: 含まれていない HelloWorld: 含まれている HelloWorld: 含まれている world: 含まれていない Come visited us at http://www.LibertyAssociates.com: 含まれていない Come visited us at http://www.LibertyAssociates.com: 含まれていない 続行するには何かキーを押してください . . .
ちなみにJavaScriptの場合。
コード(TextWrangler)
var result = ""; var str1 = "Hello", str2 = "World", str3 = "Come visit us at http://www.LibertyAssociates.com", str41= str1 + str2, str42 = str1.concat(str2), str5 = "world", str6 = str3; var strs = [str1, str2, str3, str41, str42, str5, str6]; result += "各文字列に文字「H」が含まれているかどうか\n"; for(var i = 0; i < strs.length; i++){ var msg = strs[i].indexOf("H") >= 0 ? "含まれている" : "含まれていない"; result += strs[i] + ": " + msg + "\n"; } $('#pre0').append(result);
pythonの場合。
sample.py
コード(TextWrangler)
#!/usr/bin/env python3.3 # -*- coding: utf-8 -*- s1 = "Hello" s2 = "World" s3 = "Come visit us at http://www.LibertyAssociates.com" s4 = s1 + s2 s5 = "world" s6 = s3 print("各文字列に文字「H」が含まれているかどうか") strs = [s1, s2, s3, s4, s5, s6] for s in strs: msg = "含まれていない" if s.find("H") == -1 else "含まれている" print("{0}: {1}".format(s, msg))
入出力結果(Terminal)
$ ./sample.py 各文字列に文字「H」が含まれているかどうか Hello: 含まれている World: 含まれていない Come visit us at http://www.LibertyAssociates.com: 含まれていない HelloWorld: 含まれている world: 含まれていない Come visit us at http://www.LibertyAssociates.com: 含まれていない $
0 コメント:
コメントを投稿