2012年12月14日金曜日

開発環境

『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487311-294-7)の 第15章(文字列)14.6(練習問題)練習15-1-1を解いてみる。

その他参考書籍

練習15-1-1.

コード

using System;

class Tester
{
    public void Run()
    {
        string str1 = "Hello", str2 = "World",
            str3 = @"Come visit 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("各文字列の長さ");
        foreach (string str in strs)
        {
            Console.WriteLine("{0}: {1}", str, str.Length);
        }
    }
    static void Main()
    {
        Tester t = new Tester();
        t.Run();
    }
}

入出力結果(Console Window)

各文字列の長さ
Hello: 5
World: 5
Come visit us at http://www.LibertyAssociates.com: 49
HelloWorld: 10
HelloWorld: 10
world: 5
Come visit us at http://www.LibertyAssociates.com: 49
Come visit us at http://www.LibertyAssociates.com: 49
続行するには何かキーを押してください . . .

ちなみに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 += "各文字列の長さ\n";
for(var i = 0; i < strs.length; i++){
  result += strs[i] + ": " + strs[i].length + "\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("各文字列の長さ")
strs = [s1, s2, s3, s4, s5, s6]
for s in strs:
    print("{0}: {1}".format(s, len(s)))

入出力結果(Terminal)

$ ./sample.py
各文字列の長さ
Hello: 5
World: 5
Come visit us at http://www.LibertyAssociates.com: 49
HelloWorld: 10
world: 5
Come visit us at http://www.LibertyAssociates.com: 49
$

0 コメント:

コメントを投稿