開発環境
- Microsoft Windows 7 Home Premium (OS)
- Microsoft Visual C# 2010 Express Edition (IDE)
- 言語: C#
『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487311-294-7)の第15章(文字列)の15.6(練習問題)、練習 15-2を解いてみる。
練習 15-2.
コード(Microsoft Visual C# 2010 Express Editionのエディタ)
using System;
using System.Text;
using System.Text.RegularExpressions;
class Tester
{
public void Run()
{
// 問題の文章
string str =
"We hold these truths to be self-evident,"
+ "that all men are created equal,"
+ "that they arendowed by their Creator "
+ "with certain unalienable Rights,"
+ "that among these are Life,"
+ "Liberty and the pursuit of Happiness.";
// 文章を分割するデリミタ(スペースとカンマ)の正規表現
Regex theRegex = new Regex(" |,");
// StringBuilderクラスを用意
StringBuilder output = new StringBuilder();
// カウンター値
int ctr = 1;
// 文字列を分割
foreach (string subString in theRegex.Split(str))
{
// AppendFormat()で書式付文字列を追加
output.AppendFormat("{0}: {1}\n", ctr, subString);
ctr++;
}
// 結果の文字列の配列を順に表示
Console.WriteLine(output);
}
static void Main()
{
Tester t = new Tester();
t.Run();
}
}
入出力結果(Console Window)
もう本書も数周目なので、忘れないための基礎の繰り返しという感じに。
- なので以下の書籍を合わせて読む時間も大幅に増える。
- さらに理解が深まる。
- さらにすらすらとコードを書ける。(再び1へ)
という良い循環になってきた。
合わせて読んでいる書籍。
- 正規表現の理解を深めるために本書に加えて著Jeffrey E. F. Friedl著, 株式会社ロングテール/長尾 高弘訳 『詳説 正規表現 第3版』(オライリー・ジャパン発行)もあわせて読む。
- C#の理解を深めるために本書に加えてProgramming C# 3.0, Fifth Edition, by Jesse Liberty and Donald Xie. Copyright 2008 O'Reilly Meia, Inc., 978-9-596-52743-3 (邦題『プログラミングC# 第5版』オライリー・ジャパン刊、978-4-87311-396-8)を読む。
- さらにC#を楽しみながら理解するためにHead First C# ―頭とからだで覚えるC#の基本 Andrew Stellman (著), Jennifer Greene (著), 佐藤 嘉一 (監修), 木下 哲也 (翻訳)
0 コメント:
コメントを投稿