1

I'm working at task that requires a good deal of string manipulation such as:

string result = 
    String.Format("template text {0}, {1}, ... {150}", arg1, arg2, ... ,arg150);

On account of the large number of arguments, this doesn't feel elegant and I'm worried about mistakes in argument order.

ASP.NET style templates (i.e. template text <%= arg1 %>, <%= arg2 %>, would be nice, but I don't want to have to do a web request to a web server just to get at a better templating engine.

Is there a better way to do this?

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
  • Are your args on an array and you just want to concat them separated with ',' with a "template text" at the start? – dcarneiro Mar 08 '12 at 15:04
  • How about StringBuilder or List ? – shenhengbin Mar 08 '12 at 15:05
  • 3
    Here is a duplicate: http://stackoverflow.com/questions/733378/whats-a-good-way-of-doing-string-templating-in-net. More suggestions can be found here: http://stackoverflow.com/questions/620265/can-i-set-up-html-email-templates-with-asp-net – Xaisoft Mar 08 '12 at 15:07
  • The end result will be a giant SQL script, with updates, stored proc executions, etc, so it won't only involve something that could be solved with String.Join(). Why I'm not using ADO.NET is a long story and peripheral to the question. – MatthewMartin Mar 08 '12 at 15:10

4 Answers4

5

You could try RazorEngine, based on Razor.

  string template = 
  @"<html>
      <head>
        <title>Hello @Model.Name</title>
      </head>
      <body>
        Email: @Html.TextBoxFor(m => m.Email)
      </body>
    </html>";

  var model = new PageModel { Name = "World", Email = "someone@somewhere.com" };
  string result = Razor.Parse(template, model);
jtate
  • 2,612
  • 7
  • 25
  • 35
jrummell
  • 42,637
  • 17
  • 112
  • 171
3

Modern C# 6 and Newer Applications

With the advent of C# and String Interpolation the original answers on this question have become outdated. String Interpolation is essentially syntactic sure to make string.Format. It allows you to place C# and variables inside of a string. The syntax to trigger string interpolation is to put a $ before your string quote and then anything within the curly braces will be interpolated. The following is an example that will insert a variable myName into a string.

string myName= "Scott";
string text = $"Hello {myName}!";

This is all built into C# 6 and newer applications with no separate download or library/package required.

Old/Outdated Answer

You could use a 3rd party templating framework. My favorite at the moment is Mustache due to it's simplicity and implementations in almost every language. The .NET library for it is Nustache.

The syntax for the templates is like the following:

This is my template I will insert a {{mustache}} right there.

Here's a code example for .NET.

using Nustache.Core;

string template = "This is my template I will insert a {{mustache}} right there.";
Dictionary<string, string> data = new Dictionary<string,string>();
data["mustache"] = "Beard";
string final = Render.StringToString(template, data));
scottheckel
  • 9,106
  • 1
  • 35
  • 47
0

IF you can use text templates you may put your string in a StringBuilder and then replace all occurrences of a pattern with the proper value.

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
0

I would say that if you look on the string provided by you:

"template text {0}, {1}, ... {150}"

and another

template text <%= arg1 %>, <%= arg2 %>

apart of symbolism (particular for every technology) they looks similiar. So I would say, your solution is already a good one, imo.

Or you can use "given names" in the string, like for example we do with a @parameter in SQL string. So in parameter itself you will have some notion about the argument.

Tigran
  • 61,654
  • 8
  • 86
  • 123