文字列関係

文字列関係 (C# によるプログラミング入門)
http://ufcpp.net/study/csharp/lib_string.html
string, 書式指定、正規表現


組み込み型stringの実体は、System.Stringクラス。
sを文字列とすると、以下のようなメソッドがある。

s.ToUpper, s.ToLower, s.Replace, s.Split
s.PadLeft(10) // 左寄せで10文字にする
s.PadRight
s.IndexOf // 文字列の検索


書式指定

  Console.Write(
@"
通常     {0:d}
, 区切り {0:n}
16進数   {0:x}
4桁 {0:d4}
8桁 {0:d8}
", 196);

  Console.Write(
@"
通常       {0:g}
固定桁     {0:f}
指数表記   {0:e}
パーセント {0:p}
4桁  {0:f4}
6桁  {0:f6}
桁数明示 {0:000.000}
", 0.012345678);


正規表現 System.Text.RegularExpressions.Regex, System.Text.RegularExpressions.Match

    // URL 抽出
    Regex http = new Regex(@"http://(?<domain>[\w\.]*)/(?<path>[\w\./]*)");
    Match m = http.Match(s);
    Console.Write("{0}\n", m.Value);
    Console.Write("domain: {0}\npath  : {1}\n",
      m.Groups["domain"].Value,
      m.Groups["path"].Value);

m.NextMatch()というのもある。