FrontPage > Microsoft.NET > String.Format
.NETのFormat関数
C言語のprintf関数は「"%d"」「"%f"」「"%s"」等ですが、
Format関数は、「"{0}"」「"{1}"」「"{2}"」...「"{n}"」を使用します。
整数(ゼロサプレス) | String.Format ("{n}", 5); | "5" |
文字列 | String.Format ("{n}", "TEST"); | "TEST" |
整数(0埋め[ゼロパディング]1) | String.Format ("{n:D5}", 5); | "00001" |
整数(0埋め[ゼロパディング]2) | String.Format ("{n:0000}", 5); | "0001" |
小数点(小数点2桁) | String.Format ("{n:F2}", 5.0); | "5.00" |
小数点(小数点0桁) | String.Format ("{n:F0}", 5.0); | "5" |
固定幅+右寄せ | String.Format ("{n, -5}", 5); | "5____" |
固定幅+左寄せ | String.Format ("{n, 5}", 5); | "____5" |
固定幅+(0埋め[ゼロパディング]) | String.Format ("{n, 5:D3}", 5); | "__005" |
16進数表示 | String.Format ("{n:X}", 255); | "FF" |
※上記の出力結果は、「 (スペース)」を「_」で表示しています。