Create HTML, Javascript, or XML String Variables the Easy Way Using C#

The title of this post is a bit misleading. The tips in this post can really be applied to any string, but I have found these tips to be most useful when creating markup language strings.

There are six tips applied to the HTML example below that make creating the string variable intuitive and easy to look at –

  1. Use the @ verbatim string literal syntax instead of the \ backslash character
  2. When inserting variables into the string variable, use the + concatenator before and after the variable then use the @ verbatim symbol to restart the static content of the variable
  3. If more data is necessary after the insertion of other variables, begin the new data with the @ verbatim symbol
  4. If you need to specify a double-quote (“), then escape it with an additional double-quote (Example @ <body style=””background-color:PowderBlue;””>)
  5. If you can, use single-quotes instead of double-quotes. If you use single-quotes, you will not need to escape the double-quotes. (Example: @ <p style=’font-family:verdana;color:red’>)
  6. Specify C# variables with an arbitrary prefix/suffix, then call Replace() after the string is built

1 class Class1

2 {

3 static int i = 123;

4 string s = @”

5 <html>

6 <body style=””background-color:PowderBlue;””>

7 <h1>Look! Styles and colors</h1>

8 <p style=’font-family:verdana;color:red’>Escape double-quotes with double-quotes</p>

9 <p style=’font-family:times;color:green’>

10 + i.ToString() +

11 When inserting variables, use the concatenator before and after the variable. Then, to restart the string

12 content, use the @ symbol again. Or use a prefix/suffix then call Replace() like this #i#</p>

13 <p style=’font-size:30px’>This text is 30 pixels high</p>

14 </body>

15 </html>

16 ;

17 s = s.Replace(“#i#”,i);

18 }


For a better way to use C# variables within the string variable, check out this Stack Overflow answer. For clarity, notice in our code example above we added the “#i#” inside of the string variable. Then, we Replace()’d the “#i#” with the value from the “i” variable. This works fine, however if we have many variables, it can be a bit tedious calling the Replace() method for each variable that we need to evaluate. The String Extension example at Stack Overflow, shows a useful way to handle this sort of thing.

Here’s the example of the String Extension that you can use to replace strings within a string with a list of corresponding variable names:

    1     public static class StringExtensions

   2     {

   3         public static string PHPIt<T>(this string s, string prefix, T values)

   4         {

   5             var sb = new StringBuilder(s);

   6             foreach (var p in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance))

   7             {

   8                 sb = sb.Replace(prefix + p.Name, p.GetValue(values, null).ToString());

   9             }

   10             return sb.ToString();

   11         }

   12     }

Now, you can do something like this:

    1             DateTime date = DateTime.Now;

    2             string message = "Hello World";


    4             string html = @"#message. The date is #date.";


    6             var result = StringExtensions.PHPIt(html, "#", new

    7             {

    8                 date,

    9                 message

   10             });


   12             Console.WriteLine(result);

The output would read:

Hello World. The date is 4/12/2011 2:46:43 PM.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s