開発環境
- Microsoft Windows 7 Home Premium (OS)
- Microsoft Visual C# 2010 Express Edition (IDE)
- 言語: C#
『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487311-294-7)の 第15章(文字列)15(練習問題)練習15-1-4を解いてみる。
その他参考書籍
練習15-1-4.
コード
using System;
class Tester
{
public void Run()
{
string s1 = "Hello";
string s2 = "World";
string s3 = @"Come visit us at http://www.LibertyAssociates.com";
string s41 = string.Concat(s1, s2);
string s42 = s1 + s2;
string s5 = "world";
string s61 = string.Copy(s3);
string s62 = s3;
string[] strings = { s1, s2, s3, s41, s42, s5, s61, s62 };
Console.WriteLine("s2, 「{0}」と同じかどうか(順に「==」、string.Equals(str1,str2)、str1.Equals(str2)を使用", s2);
foreach (string item in strings)
{
string msg1 = item == s2 ? "同じ" : "異なる";
string msg2 = string.Equals(item, s2) ? "同じ" : "異なる";
string msg3 = s2.Equals(item) ? "同じ" : "異なる";
Console.WriteLine("{0}: {1}, {2}, {3}", item, msg1, msg2, msg3);
}
}
static void Main()
{
Tester t = new Tester();
t.Run();
}
}
入出力結果(Console Window)
s2, 「World」と同じかどうか(順に「==」、string.Equals(str1,str2)、str1.Equals(st r2)を使用 Hello: 異なる, 異なる, 異なる World: 同じ, 同じ, 同じ Come visit us at http://www.LibertyAssociates.com: 異なる, 異なる, 異なる HelloWorld: 異なる, 異なる, 異なる HelloWorld: 異なる, 異なる, 異なる world: 異なる, 異なる, 異なる Come visit us at http://www.LibertyAssociates.com: 異なる, 異なる, 異なる Come visit us at http://www.LibertyAssociates.com: 異なる, 異なる, 異なる 続行するには何かキーを押してください . . .
0 コメント:
コメントを投稿