開発環境
- Microsoft Windows 8 Pro 64bit 日本語 (OS)
- Microsoft Visual Studio Express 2012 for Windows Desktop (IDE)
- プログラミング言語: C#
『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487312-194-7)の 第15章(文字列)15.6(練習問題)練習15-1-4を解いてみる。
その他参考書籍
練習15-1-4.
コード
using System;
class Tester
{
public void Run()
{
string s1 = "Hello",
s2 = "World",
s3 = @"Come visit us at http://www.LibertyAssociates.com",
s4 = s1 + s2,
s5 = "world",
s6 = s3;
string[] strings = { s1, s2, s3, s4, s5, s6 };
Console.WriteLine("文字列「{0}」と同じかどうか", s2);
foreach (string s in strings)
{
string msg = s == s2 ? "同じ" : "異なる";
Console.WriteLine("{0}: {1}", s, msg);
}
}
static void Main()
{
Tester t = new Tester();
t.Run();
}
}
入出力結果(Console Window)
文字列「World」と同じかどうか Hello: 異なる World: 同じ Come visit us at http://www.LibertyAssociates.com: 異なる HelloWorld: 異なる world: 異なる Come visit us at http://www.LibertyAssociates.com: 異なる 続行するには何かキーを押してください . . .
ちなみにJavaScriptの場合。
コード(BBEdit)
var s1 = "Hello";
var s2 = "World";
var s3 = "Come visit us at http://www.LibertyAssociates.com";
var s4 = s1 + s2;
var s5 = "world";
var s6 = s3;
var strings = [s1, s2, s3, s4, s5, s6];
var result = "文字列「" + s2 + "」と同じかどうか\n";
for(var i = 0, max = strings.length ; i < max; i++){
var msg = s2 === strings[i] ? "同じ" : "異なる";
result += strings[i] + ": " + msg + "\n";
}
$('#pre0').text(result);
pythonの場合。
sample.py
コード(BBEdit)
#!/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
strings = [s1, s2, s3, s4, s5, s6]
print("文字列に「{0}」と同じかどうか".format(s2))
for s in strings:
result = "同じ" if s == s2 else "異なる"
print("{0}: {1}".format(s, result))
入出力結果(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 コメント:
コメントを投稿