D version of C# code

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Apr 16 02:46:13 PDT 2017


On 04/16/2017 11:20 AM, Joel wrote:
> What would you put instead of this C# code, in D?
>
> ```C#
>           // Arrange
>             const string templateString = "My {pet} has {number}
> {ailment}.";
>             var pairs = new
>             {
>                 pet = "dog",
>                 number = 5,
>                 ailment = "fleas",
>             };
>
>             // Act
>             var result =
> TemplateStringInterpolator.ReplaceTokens(templateString, pairs);
>
>             // Assert
>             result.Should().Be("My dog has 5 fleas.");
> ```

void main()
{
     // Arrange
     const string templateString = "My {pet} has {number} {ailment}.";
     auto pairs = [
         "pet": "dog",
         "number": "5",
         "ailment": "fleas",
     ];

     // Act
     import std.regex: regex, replaceAll;
     auto result = templateString
         .replaceAll!(m => pairs[m[1]])(regex(`\{([^}]+)\}`));

     // Assert
     assert(result == "My dog has 5 fleas.");
}



More information about the Digitalmars-d-learn mailing list