2018年6月1日金曜日

開発環境

初めてのC# 第2版 (Jesse Liberty (著)、Brian MacDonald (著)日向 俊二 (翻訳)、オライリージャパン)の15章(文字列)、15.6(練習問題)、練習15-2.を取り組んでみる。

コード

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {
        string s = 
            "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, " +
            "that among these are Life, " +
            "Liberty and the pursuit of Happiness.";
        Regex regex = new Regex(@"\b\w+\b");

        foreach (var item in regex.Matches(s))
        {
            Console.WriteLine(item);
        }
    }
}

入出力結果(コマンドプロンプト)

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
that
among
these
are
Life
Liberty
and
the
pursuit
of
Happiness
続行するには何かキーを押してください . . .

0 コメント:

コメントを投稿