Let me preface this article with the statement that I am by no means a 'good' C# programmer. I've used it a bit at work (we're currently transitioning to it) so I've seen it here and there. I know a fair amount about Classic ASP however, so when I noticed a neat little feature in C#/ASP.NET I wanted to see if I could mimic it in other languages.

AppendFormat Method

The method in C# is called AppendFormat. It belongs to the System.text namespace which contains the StringBuilder class and AppendFormat is a method of this. Here's the MSDN article if you'd like to find out more about it.

Old School Method

The neat thing about this method is it's an alternative to concatenation. Concatenation is basically the process of combining text and variables together like so:

var a = "lorem ipsum dolor" var b = "adipiscing elit" document.write(a + " sit amet consectetur " + b)

I think we're all pretty familiar with the code above. We're also probably familiar with some of the annoyances of the code above such as the fact that every time we combine text with a string variable, we need to start a quotation mark. Quotation marks are bothersome because sometimes (or rather often times in my case) we forget to properly close the ending quotation mark resulting in an error. More trouble can ensue when we have to start re-arranging variables and text within the concatenated string.

Another problem is I often forget to include the concatenation symbol in between my text and variables. This, if anything, is probably the number one mistake I make.

Emulation Using Javascript

As I reviewed my co-workers C# code, I felt that perhaps C# had a good point so I set out to emulate this in other languages.

First of all, below is the syntax we use for this alternative method. We use keyword itentifiers i.e. {NUMBER} to indicate an item should be placed in its spot. That number corresponds with the index of the array items on the right i.e NEAL, GROSSKOPF. The neat thing with this is we can easily move {0} arround without having to worry about closing the quotation marks and there is no need to use the concatenation symbol...ever!

AppendFormat("Lorem ipsum dolor sit {0} {1} amet, consectetur {0} adipiscing {0} elit.", ["NEAL", "GROSSKOPF"])

Below is the function I wrote to emulate this in Javascript. I pass the function an array, loop through it, and replace the placeholders with the array item in the same index.

<script type="text/javascript"> function AppendFormat(input,arr) { for (i = 0; i < arr.length; i++) { while (input.indexOf("{"+i+"}") != -1) { input = input.replace("{"+i+"}", arr[i]); } } return input; } document.write(AppendFormat("Lorem ipsum dolor sit {0} {1} amet, consectetur {0} adipiscing {0} elit.", ["NEAL", "GROSSKOPF"])); </script>

Easily Re-Arrange Items

The great thing about this, is we can easily re-arrange items simply by moving the placeholder around in the text block like so:

AppendFormat("Lorem ipsum dolor sit {1} {0} amet, consectetur {1} adipiscing {0} elit.", ["NEAL", "GROSSKOPF"]);

Classic ASP Variant

I also wanted to create the same function but in Classic ASP, my language of choice:

function AppendFormat(input,arr) for i = 0 to ubound(arr) input = replace(input,"{" & i & "}",arr(i)) next AppendFormat = input end function response.Write AppendFormat("Lorem ipsum dolor sit {0} {1} amet, consectetur {1} adipiscing {1} elit.", array("NEAL","GROSSKOPF"))

As you can see, this is pretty much the basic princple that we used for the Javascript version.

Conclusion

I'm sure this article isn't as exciting as some of my others, and for all I know, most my readers are much better programmers than myself but I thought it was a cool idea so I wrote about it.