開発環境
- 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-5を解いてみる。
その他参考書籍
練習15-1-5.
コード
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("各文字列が文字列「0」と同じかどうか(大文字/小文字は無視)", str2); foreach (string str in strs) { string msg = str2.ToLower() == str.ToLower() ? "同じ" : "異なる"; Console.WriteLine("{0}: {1}", str, msg); } } static void Main() { Tester t = new Tester(); t.Run(); } }
入出力結果(Console Window)
各文字列が文字列「0」と同じかどうか(大文字/小文字は無視) 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 += "文字列「" + str2 + "」と同じかどうか(大文字/小文字は無視)\n"; for(var i = 0; i < strs.length; i++){ var msg = str2.toLowerCase() === strs[i].toLowerCase() ? "同じ" : "異なる"; 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("各文字列が文字列{0}と同じかどうか".format(s2)) strs = [s1, s2, s3, s4, s5, s6] for s in strs: msg = "同じ" if s2.lower() == s.lower() else "異なる" print("{0}: {1}".format(s, msg))
入出力結果(Terminal)
$ ./sample.py 各文字列が文字列Worldと同じかどうか Hello: 異なる World: 同じ Come visit us at http://www.LibertyAssociates.com: 異なる HelloWorld: 異なる world: 同じ Come visit us at http://www.LibertyAssociates.com: 異なる $
0 コメント:
コメントを投稿