開発環境
- Microsoft Windows 8 Pro 64bit 日本語 (OS)
- Microsoft Visual C# 2010 Express Edition (IDE)
- 言語: C#
『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487311-294-7)の 第15章(文字列)15.6(練習問題)練習15-1-5を解いてみる。
その他参考書籍
練習15-1-5.
コード
using System;
class Tester
{
public void Run()
{
string str1 = "Hello";
string str2 = "World";
string str3 = @"Come visit us at http://www.LibertyAssociates.com";
string str41 = string.Concat(str1, ", ", str2, "!");
string str42 = str1 + ", " + str2 + "!";
string str5 = "world";
string str61 = string.Copy(str3);
string str62 = str3;
string[] strs = { str1, str2, str3, str41, str42, str5, str61, str62 };
Console.WriteLine("「{0}」と同じ文字列かどうか", str2);
foreach (string item in strs)
{
string msg1 = str2.ToLower() == item.ToLower() ? "同じ" : "異なる";
string msg2 = String.Compare(str2, item, true) == 0 ? "同じ" : "異なる";
Console.WriteLine("{0}: {1} {2}", item, msg1, msg2);
}
}
static void Main()
{
Tester t = new Tester();
t.Run();
}
}
入出力結果(Console Window)
「World」と同じ文字列かどうか Hello: 異なる 異なる World: 同じ 同じ Come visit us at http://www.LibertyAssociates.com: 異なる 異なる Hello, World!: 異なる 異なる Hello, World!: 異なる 異なる world: 同じ 同じ Come visit us at http://www.LibertyAssociates.com: 異なる 異なる Come visit us at http://www.LibertyAssociates.com: 異なる 異なる 続行するには何かキーを押してください . . .
ちなみにJavaScriptの場合。
コード(TextWrangler)
var str1 = "Hello";
var str2 = "World";
var str3 = "Come visit us at http://www.LibertyAssociates.com";
var str41 = str1.concat(", ", str2, "!");
var str42 = str1 + ", " + str2 + "!";
var str5 = "world";
var str6 = str3;
var strs = [str1, str2, str3, str41, str42, str5, str6];
var result = "「" + str2 + "」と同じ文字列かどうか\n";
for(var i = 0; i < strs.length; i++){
var msg = str2.toLowerCase() === strs[i].toLowerCase() ? "同じ" : "異なる";
result += strs[i] + ": " + msg + "\n";
}
$('#pre0').text(result);
pythonの場合。
sample.py
コード(TextWrangler)
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
str1 = "Hello"
str2 = "World"
str3 = """Come visit us at http://www.LibertyAssociates.com"""
str4 = str1 + ", " + str2 + "!"
str5 = "world"
str6 = str3
strs = [str1, str2, str3, str4, str5, str6]
print("「{0}」と同じ文字列か(大文字/小文字を無視)".format(str2))
for str in strs:
msg = "同じ" if str2.lower() == str.lower() else "異なる"
print("{0}: {1}".format(str, msg))
入出力結果(Terminal)
$ ./sample.py 「World」と同じ文字列か(大文字/小文字を無視) Hello: 異なる World: 同じ Come visit us at http://www.LibertyAssociates.com: 異なる Hello, World!: 異なる world: 同じ Come visit us at http://www.LibertyAssociates.com: 異なる $
0 コメント:
コメントを投稿