2012年8月7日火曜日

開発環境

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

練習15-2.

コード

using System;
using System.Text;
using System.Text.RegularExpressions;

namespace Sample
{
    class Tester
    {
        public void Run()
        {
            string str = "We hold these truths to be self-evident," +
                "that all men are created equal," +
                "that they are endowed by their Creator with certain unalienable Rights," +
                "Liberty and the pursuit of Happiness.";
            StringBuilder sb = new StringBuilder();
            Regex reg = new Regex(",| ");
            int id = 0;
            foreach (string item in reg.Split(str))
            {
                sb.AppendFormat("{0}: {1}\n", id, item);
                id++;
            }
            Console.WriteLine(sb);
        }
        static void Main()
        {
            Tester t = new Tester();
            t.Run();
        }
    }
}

入出力結果(Console Window)

0: We
1: hold
2: these
3: truths
4: to
5: be
6: self-evident
7: that
8: all
9: men
10: are
11: created
12: equal
13: that
14: they
15: are
16: endowed
17: by
18: their
19: Creator
20: with
21: certain
22: unalienable
23: Rights
24: Liberty
25: and
26: the
27: pursuit
28: of
29: Happiness.

続行するには何かキーを押してください . . .

0 コメント:

コメントを投稿