2012年11月1日木曜日

開発環境

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

その他参考書籍

練習15-1-1.

コード

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("各文字列の長さ");
        foreach (string item in strs)
        {
            Console.WriteLine("{0}: {1}", item, item.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
Hello, World!: 13
Hello, World!: 13
world: 5
Come visit us at http://www.LibertyAssociates.com: 49
Come visit us at http://www.LibertyAssociates.com: 49
続行するには何かキーを押してください . . .

ちなみに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 = "各文字列の長さ\n";
for(var i = 0; i < strs.length; i++){
  result += strs[i] + ": " + strs[i].length + "\n";
}
$('#pre0').text(result);







						

0 コメント:

コメントを投稿