D version of C# code

Joel via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Apr 16 02:20:12 PDT 2017


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.");
```

I've made a function for strings:

```D
string replaceTokens(in string tmpString, in string[string] aa) {
     import std.array : replace;

     string result = tmpString;

     foreach(key, value; aa) {
         string addBits(in string root) {
             return "{" ~ root ~ "}";
         }
         result = result.replace(addBits(key), value);
     }

     return result;
}
```



More information about the Digitalmars-d-learn mailing list