Last active
December 7, 2015 08:41
-
-
Save masaru-b-cl/1dee4be17a7fd878d4c2 to your computer and use it in GitHub Desktop.
文字列補完(string interpolation)のパターンあれこれ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace StringInterpolation | |
{ | |
[TestClass] | |
public class StringInterpolationTest | |
{ | |
[TestMethod] | |
public void 文字列の埋め込み() | |
{ | |
// $"..."で括った文字列が対象 | |
// 埋め込む箇所に{}で括って対象の式を指定する | |
var name = "Sho"; | |
Assert.AreEqual("Hello Sho!", $"Hello {name}!"); | |
} | |
[TestMethod] | |
public void 文字列の空白埋め() | |
{ | |
// {式,桁数}のように,に続けて桁数を指定する | |
var name = "Sho"; | |
Assert.AreEqual(" Sho is first name", $"{name,10} is first name"); | |
} | |
[TestMethod] | |
public void 暗黙型変換() | |
{ | |
// 文字列以外はToStringの結果が設定される | |
var age = 35; | |
Assert.AreEqual("age is 35", $"age is {age}"); | |
// なので、DateTime型などはそのままだと時刻まで入ってしまう | |
var birthday = new DateTime(1980, 10, 24); | |
Assert.AreEqual("Birthday is 1980/10/24 0:00:00", $"Birthday is {birthday}"); | |
} | |
[TestMethod] | |
public void コロンを含む式の利用() | |
{ | |
// 対象の式を()で括る | |
var age = 35; | |
Assert.AreEqual("you are middle age", | |
$"you are {(age <= 20 ? "young" : age > 45 ? "senior" : "middle age")}"); | |
// 改行は含められない | |
// Assert.AreEqual("you are middle age", | |
// $"you are {(age < 30 ? "young" : | |
// age > 45 ? "senior" : | |
// "middle age")}"); | |
} | |
[TestMethod] | |
public void 中括弧を含む式の利用() | |
{ | |
// {{, }}のように二重にする | |
var name = "Sho"; | |
Assert.AreEqual("{Sho}", $"{{{name}}}"); | |
} | |
[TestMethod] | |
public void 日付のフォーマット() | |
{ | |
// :(コロン)に続けて日付の編集書式を指定する | |
var birthday = new DateTime(1980, 10, 24); | |
Assert.AreEqual("Birthday is 1980-10-24", $"Birthday is {birthday:yyyy-MM-dd}"); | |
} | |
[TestMethod] | |
public void 数値のフォーマット() | |
{ | |
// :(コロン)に続けて数値の編集書式を指定する | |
// 1) 小数点以下桁数指定 | |
var height = 173m; | |
Assert.AreEqual("Height is 173.00(cm)", $"Height is {height:0.00}(cm)"); | |
// 2) カンマ編集 | |
var wantsSalary = 300000m; | |
Assert.AreEqual("wants 300,000", $"wants {wantsSalary:#,#}"); | |
// 3) 数字の桁指定 | |
var serialNo = 123; | |
Assert.AreEqual("my serial is 000123", $"my serial is {serialNo:d6}"); | |
// 4) 動的桁指定 | |
var digits = 10; | |
// !! 書式指定文字列の中で文字列補完は使えない !! | |
// Assert.AreEqual("long serial 0000000123", $"long serial {serialNo:d{digits}}"); | |
// a) String.Formatと組み合わせるか | |
Assert.AreEqual("long serial 0000000123", | |
String.Format($"long serial {$"{{0:d{digits}}}"}", serialNo)); | |
// b) {}の中でToStringする | |
Assert.AreEqual("long serial 0000000123", | |
$"long serial {serialNo.ToString($"d{digits}")}"); | |
// 【緩募】もっと良い動的な書式指定文字列の指定方法 | |
} | |
[TestMethod] | |
public void オブジェクトのメンバーの利用() | |
{ | |
// .でオブジェクトのメンバーにもアクセスできる | |
var person = new { Name = "Sho", Age = 35, Married = true }; | |
Assert.AreEqual("name:Sho, age:35 (既婚)", | |
$"name:{person.Name}, age:{person.Age} ({(person.Married ? "既婚" : "未婚")})"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment