2012年12月15日土曜日

開発環境

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

その他参考書籍

練習15-1-2.

コード

using System;

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

入出力結果(Console Window)

各文字列の3盤目の文字列
Hello: l
World: r
Come visited us at http://www.LibertyAssociates.com: m
HelloWorld: l
HelloWorld: l
world: r
Come visited us at http://www.LibertyAssociates.com: m
Come visited us at http://www.LibertyAssociates.com: m
続行するには何かキーを押してください . . .

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

入出力結果(Terminal)

$ ./sample.py
各文字列の3番目の文字
Hello: l
World: r
Come visit us at http://www.LibertyAssociates.com: m
HelloWorld: l
world: r
Come visit us at http://www.LibertyAssociates.com: m
$

0 コメント:

コメントを投稿